From 54d97ec3b29eb948b506f07c378f210cebadabe3 Mon Sep 17 00:00:00 2001 From: Alison Date: Fri, 18 Aug 2017 15:43:16 -0400 Subject: [PATCH] change pie chart colors (re #240) Incorporates: fix pie chart multiple colors changed (re #242) --- .../visualizations/chart/chart-editor.html | 38 +++++++++++- client/app/visualizations/chart/index.js | 58 ++++++++++++++----- .../app/visualizations/chart/plotly/utils.js | 12 +++- 3 files changed, 90 insertions(+), 18 deletions(-) diff --git a/client/app/visualizations/chart/chart-editor.html b/client/app/visualizations/chart/chart-editor.html index 202b92f0a9..fa2a7657c9 100644 --- a/client/app/visualizations/chart/chart-editor.html +++ b/client/app/visualizations/chart/chart-editor.html @@ -9,11 +9,14 @@
  • Y Axis
  • -
  • +
  • Series
  • Data Labels +
  • + Colors
  • @@ -329,4 +332,35 @@

    {{$index == 0 ? 'Left' : 'Right'}} Y Axis

    placeholder="(auto)">
    - \ No newline at end of file +
    + + + + + + + + + + + + + +
    zIndexLabelColor
    + + {{name}} + + + + + + + + + + +
    +
    + + diff --git a/client/app/visualizations/chart/index.js b/client/app/visualizations/chart/index.js index 8797501220..409de19a32 100644 --- a/client/app/visualizations/chart/index.js +++ b/client/app/visualizations/chart/index.js @@ -156,19 +156,48 @@ function ChartEditor(ColorPalette, clientConfig) { } function refreshSeries() { - const seriesNames = map(scope.queryResult.getChartData(scope.options.columnMapping), i => i.name); - const existing = keys(scope.options.seriesOptions); - each(difference(seriesNames, existing), (name) => { - scope.options.seriesOptions[name] = { - type: scope.options.globalSeriesType, - yAxis: 0, - }; - scope.form.seriesList.push(name); - }); - each(difference(existing, seriesNames), (name) => { - scope.form.seriesList = without(scope.form.seriesList, name); - delete scope.options.seriesOptions[name]; - }); + // for pie charts only get color list (x row) instead of series list (y column) + if (scope.options.globalSeriesType === 'pie') { + const seriesData = scope.queryResult.getData(); + scope.form.colorsList = []; + const xColumnName = scope.form.xAxisColumn; + seriesData.forEach((rowOfData) => { + scope.form.colorsList.push(rowOfData[xColumnName]); + }); + + const colorNames = scope.form.colorsList; + let existing = []; + if (scope.options.colorOptions === undefined) { + existing = colorNames; + } else { + existing = keys(scope.options.colorOptions); + } + each(difference(colorNames, existing), (name) => { + scope.options.colorOptions[name] = { + type: scope.options.globalSeriesType, + yAxis: 0, + }; + scope.form.colorsList.push(name); + }); + each(difference(existing, colorNames), (name) => { + scope.form.colorsList = without(scope.form.colorsList, name); + delete scope.options.colorOptions[name]; + }); + } else { + const seriesNames = map(scope.queryResult.getChartData(scope.options.columnMapping), i => i.name); + const existing = keys(scope.options.seriesOptions); + each(difference(seriesNames, existing), (name) => { + scope.options.seriesOptions[name] = { + type: scope.options.globalSeriesType, + yAxis: 0, + }; + scope.form.seriesList.push(name); + }); + each(difference(existing, seriesNames), (name) => { + scope.form.seriesList = without(scope.form.seriesList, name); + delete scope.options.seriesOptions[name]; + }); + } } function setColumnRole(role, column) { @@ -200,6 +229,8 @@ function ChartEditor(ColorPalette, clientConfig) { yAxisColumns: [], seriesList: sortBy(keys(scope.options.seriesOptions), name => scope.options.seriesOptions[name].zIndex), + colorsList: sortBy(keys(scope.options.colorOptions), name => + scope.options.colorOptions[name].zIndex), }; scope.$watchCollection('form.seriesList', (value) => { @@ -209,7 +240,6 @@ function ChartEditor(ColorPalette, clientConfig) { }); }); - scope.$watchCollection('form.yAxisColumns', (value, old) => { each(old, unsetColumn); each(value, partial(setColumnRole, 'y')); diff --git a/client/app/visualizations/chart/plotly/utils.js b/client/app/visualizations/chart/plotly/utils.js index c118f49f75..1b9c94f75d 100644 --- a/client/app/visualizations/chart/plotly/utils.js +++ b/client/app/visualizations/chart/plotly/utils.js @@ -252,10 +252,18 @@ function preparePieData(seriesList, options) { return { values: map(serie.data, i => i.y), - labels: map(serie.data, row => (hasX ? normalizeValue(row.x) : `Slice ${index}`)), + labels: map(serie.data, (row, rowIdx) => { + + const rowX = hasX ? normalizeValue(row.x) : `Slice ${index}`; + const rowOpts = options.seriesOptions[rowX]; + if (rowOpts) { + colorPalette[rowIdx] = rowOpts.color; + } + return rowX; + }), type: 'pie', hole: 0.4, - marker: { colors: ColorPaletteArray }, + marker: { colors: colorPalette }, hoverinfo, text: [], textinfo: options.showDataLabels ? 'percent' : 'none',