Browse Source

fix color formatter

pull/1/head
vargburz 2 years ago
parent
commit
4d41cb5373
  1. 17
      examples/demo.html
  2. 15
      src/index.ts
  3. 21
      src/types.ts

17
examples/demo.html

@ -13,10 +13,10 @@
var pod = new ChartwerkBarPod( var pod = new ChartwerkBarPod(
document.getElementById('chart'), document.getElementById('chart'),
[ [
{ target: 'test11', datapoints: getData(), matchedKey: 'm-1', color: 'red' }, { target: 'test11', datapoints: getData(), matchedKey: 'm-1', color: 'red', colorFormatter: (data) => ['green', 'yellow'][data.rowIndex] },
{ target: 'test12', datapoints: [[100, 10], [200, 20], [300, 10]], matchedKey: 'm-1', color: 'green' }, // { target: 'test12', datapoints: [[100, 10], [200, 20], [300, 10]], matchedKey: 'm-1', color: 'green' },
{ target: 'test21', datapoints: [[130, 10], [230, 26], [330, 15]], matchedKey: 'm-2', color: 'yellow'}, // { target: 'test21', datapoints: [[130, 10], [230, 26], [330, 15]], matchedKey: 'm-2', color: 'yellow'},
{ target: 'test22', datapoints: [[130, 10], [230, 27], [330, 10]], matchedKey: 'm-2', color: 'blue' }, // { target: 'test22', datapoints: [[130, 10], [230, 27], [330, 10]], matchedKey: 'm-2', color: 'blue' },
], ],
{ {
usePanning: false, usePanning: false,
@ -24,8 +24,8 @@
x: { format: 'custom', invert: false, valueFormatter: (value) => { return 'L' + value; } }, x: { format: 'custom', invert: false, valueFormatter: (value) => { return 'L' + value; } },
y: { format: 'custom', invert: false, range: [0, 30], valueFormatter: (value) => { return value + '%'; } } y: { format: 'custom', invert: false, range: [0, 30], valueFormatter: (value) => { return value + '%'; } }
}, },
stacked: true, stacked: false,
matching: true, matching: false,
maxBarWidth: 20, maxBarWidth: 20,
minBarWidth: 4, minBarWidth: 4,
zoomEvents: { zoomEvents: {
@ -37,7 +37,8 @@
], ],
eventsCallbacks: { eventsCallbacks: {
zoomIn: (range) => { console.log('range', range) } zoomIn: (range) => { console.log('range', range) }
} },
renderLegend: false,
} }
); );
console.time('render'); console.time('render');
@ -46,7 +47,7 @@
function getData() { function getData() {
return [ return [
[100, 15], [110, 20], [300, 10], [100, 15], [200, 20], [300, 10],
]; ];
} }
</script> </script>

15
src/index.ts

@ -59,12 +59,12 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
.data(data) .data(data)
.enter().append('g') .enter().append('g')
.attr('class', 'rects-container') .attr('class', 'rects-container')
.each((d: RowValues, i: number, nodes: any) => { .each((d: RowValues, rowIndex: number, nodes: any) => {
const container = d3.select(nodes[i]); const container = d3.select(nodes[rowIndex]);
container.selectAll('rect') container.selectAll('rect')
.data(d.values) .data(d.values)
.enter().append('rect') .enter().append('rect')
.style('fill', (val, i) => d.colors[i]) .style('fill', (val, idx) => this.getBarColor(d, val, idx, rowIndex))
.attr('opacity', () => this.getBarOpacity(d)) .attr('opacity', () => this.getBarOpacity(d))
.attr('x', (val: number, idx: number) => { .attr('x', (val: number, idx: number) => {
return this.getBarPositionX(d.key, idx); return this.getBarPositionX(d.key, idx);
@ -150,6 +150,13 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
return this.options.barOptions.opacityFormatter(rowValues); return this.options.barOptions.opacityFormatter(rowValues);
} }
getBarColor(rowValues: RowValues, val: number, i: number, rowIndex: number): string {
if(_.isFunction(rowValues.colors[i])) {
return (rowValues.colors[i] as Function)({ rowData: rowValues, val, stackedIndex: i, rowIndex });
}
return (rowValues.colors[i] as string);
}
mergeMacthedSeriesAndSort(matchedSeries: any[]) { mergeMacthedSeriesAndSort(matchedSeries: any[]) {
// TODO: refactor // TODO: refactor
if(matchedSeries.length === 0) { if(matchedSeries.length === 0) {
@ -197,7 +204,7 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
const additionalValuesColumns = _.map(series, serie => _.map(serie.datapoints, row => row[2] !== undefined ? row[2] : null)); const additionalValuesColumns = _.map(series, serie => _.map(serie.datapoints, row => row[2] !== undefined ? row[2] : null));
const zippedAdditionalValuesColumn = _.zip(...additionalValuesColumns); const zippedAdditionalValuesColumn = _.zip(...additionalValuesColumns);
const zippedValuesColumn = _.zip(...valuesColumns); const zippedValuesColumn = _.zip(...valuesColumns);
const colors = _.map(series, serie => serie.color); const colors = _.map(series, serie => serie.colorFormatter || serie.color);
const tagrets = _.map(series, serie => serie.target); const tagrets = _.map(series, serie => serie.target);
const zippedData = _.zip(keysColumn, zippedValuesColumn, zippedAdditionalValuesColumn, tagrets); const zippedData = _.zip(keysColumn, zippedValuesColumn, zippedAdditionalValuesColumn, tagrets);
const data = _.map(zippedData, row => { return { key: row[0], values: row[1], additionalValues: row[2], colors, serieTarget: tagrets } }); const data = _.map(zippedData, row => { return { key: row[0], values: row[1], additionalValues: row[2], colors, serieTarget: tagrets } });

21
src/types.ts

@ -3,6 +3,7 @@ import { Serie, Options } from '@chartwerk/core';
export type BarSerieParams = { export type BarSerieParams = {
matchedKey: string; matchedKey: string;
colorFormatter: (serie: BarSerie) => string; colorFormatter: (serie: BarSerie) => string;
label: BarLabel;
} }
export type BarSerie = Serie & Partial<BarSerieParams>; export type BarSerie = Serie & Partial<BarSerieParams>;
export type BarAdditionalOptions = { export type BarAdditionalOptions = {
@ -29,6 +30,24 @@ export type RowValues = {
key: number, key: number,
values: number[], values: number[],
additionalValues: (null | number)[], // values in datapoints third column additionalValues: (null | number)[], // values in datapoints third column
colors: string[], colors: (string | ((data: any) => string))[],
serieTarget: string[], serieTarget: string[],
} }
export enum BarLabelPosition {
TOP = 'top',
CENTER = 'center',
BOTTOM = 'bottom',
}
export enum BarLabelPlaceholder {
TRIANGLE = 'triangle',
}
export type BarLabel = {
position: BarLabelPosition;
offset: number;
value: string;
valueFormatter: (data: any) => string;
placeholder: BarLabelPlaceholder;
}

Loading…
Cancel
Save