Skip to content

Commit

Permalink
Chart js work
Browse files Browse the repository at this point in the history
  • Loading branch information
panda01 committed May 9, 2016
1 parent 7dd90bd commit 104e68d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/ui/public/vislib/styles/_legend.less
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ visualize-legend {
flex-direction: row;
padding-top: 5px;

&.fixed-width {
width: 200px;
}

.header {
cursor: pointer;
width: 15px;
Expand Down
8 changes: 5 additions & 3 deletions src/ui/public/visualize/visualize.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ <h4>No results found</h4>
class="visualize-chart"></div>
<visualize-legend></visualize-legend>
</div>
<canvas id="canvas-chart">
</canvas>
<div id="chart-legend"></div>
<div class="visualize-chart">
<canvas id="canvas-chart" class="visualize-chart">
</canvas>
<div id="chart-legend" class="fixed-width legend-col-wrapper"></div>
</div>
<!-- <pre>{{chartData | json}}</pre> -->
<visualize-spy ng-if="vis.type.requiresSearch && showSpyPanel"></visualize-spy>
23 changes: 17 additions & 6 deletions src/ui/public/visualize/visualize.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ uiModules
}
return '#' + hexStr;
}
const isPieChart = (chartType === 'pie');
function decodeBucketData(buckets, aggregations) {
const chartDatasetConfigs = {};
if (!buckets) { return chartDatasetConfigs; }
Expand Down Expand Up @@ -64,17 +65,21 @@ uiModules
decodeBucket(buckets[0], aggregations[buckets[0].id]);
const arrDatasets = buckets.map(bucket => { return chartDatasetConfigs[bucket.id]; });
// Make some colors for all of the data points.
// This need to be different, instead of looking at all the colors
// you should look at RGB separate and limit the number from there
// then multiply to get your result
arrDatasets.forEach(set => {
const colorOffset = 10000;
const allTheColors = Math.pow(16, 6);
const colorOffset = allTheColors / 8;
let numDataPoints = set.data.length;
const maxColors = Math.pow(16,6) - (colorOffset * 2);
const maxColors = allTheColors - (colorOffset * 2);
const difference = maxColors / numDataPoints;
let currColor = colorOffset;
do {
while (numDataPoints-- > 0) {
set.backgroundColor.push(makeColor(currColor));
currColor += difference;
} while (--numDataPoints > 0);
if (chartType !== 'pie') {
}
if (!isPieChart) {
set.backgroundColor = set.backgroundColor[0];
}
});
Expand All @@ -86,7 +91,7 @@ uiModules
const aggConfigMap = aggConfigs.byId;
const decodedData = decodeBucketData(aggConfigs.bySchemaGroup.buckets, esResp.aggregations);
const flattenedLabels = _.reduce(decodedData, (prev, dataset) => {
return prev.concat(dataset.labels);
return _.uniq(prev.concat(dataset.labels));
}, []);
if (myChart) { // Not a fan of this, i should be using update
myChart.destroy();
Expand All @@ -98,6 +103,12 @@ uiModules
datasets: decodedData
},
options: {
legendCallback: function (chartObj) {
const multipleBuckets = aggConfigs.bySchemaGroup.buckets.length > 1;
const legendItems = multipleBuckets || isPieChart ? flattenedLabels : [aggConfigs.bySchemaGroup.metrics[0]._opts.type];
const itemsHtmlArr = legendItems.map(item => { return '<li>' + item + '</li>'; });
return '<ul>' + itemsHtmlArr.join('') + '</ul>';
},
legend: {
display: false
},
Expand Down

0 comments on commit 104e68d

Please sign in to comment.