Skip to content

Commit f367f83

Browse files
fix(scatter): custom point colors demo
1 parent a160159 commit f367f83

File tree

8 files changed

+43
-15
lines changed

8 files changed

+43
-15
lines changed

Diff for: packages/core/demo/chart-types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
donutData,
1818
// Line
1919
lineTimeSeriesOptions,
20+
lineTimeSeriesWithCustomStroke,
2021
lineTimeSeriesData,
2122
lineData,
2223
lineOptions,
@@ -83,6 +84,11 @@ export const chartTypes = [
8384
options: lineTimeSeriesOptions,
8485
data: lineTimeSeriesData
8586
},
87+
{
88+
id: "line-time-series-customstroke",
89+
options: lineTimeSeriesWithCustomStroke,
90+
data: lineTimeSeriesData
91+
},
8692
{
8793
id: "line",
8894
options: lineOptions,

Diff for: packages/core/demo/demo-data/line.ts

+18
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ export const lineTimeSeriesOptions = {
125125
curve: "curveMonotoneX"
126126
};
127127

128+
export const lineTimeSeriesWithCustomStroke = {
129+
...lineTimeSeriesOptions,
130+
title: "Line (custom stroke, fill colors)",
131+
getStrokeColor: (datasetLabel, label, value, datum, originalStrokeColor)=>{
132+
console.log(`originalStrokeColor: ${originalStrokeColor}`);
133+
return value > 60000 ? '#FF0000' : originalStrokeColor;
134+
},
135+
getFillColor: (datasetLabel, label, value, datum, originalFillColor)=>{
136+
console.log(`originalFillColor: ${originalFillColor}`);
137+
return value > 60000 ? '#FF0000' : originalFillColor;
138+
},
139+
getIsFilled: (datasetLabel, label, value, datum, originalIsFilled)=>{
140+
console.log(`originalIsFilled: ${originalIsFilled}`);
141+
return value > 60000 ? true : originalIsFilled;
142+
}
143+
};
144+
128145
export const lineData = {
129146
labels: ["Qty", "More", "Sold", "Restocking", "Misc"],
130147
datasets: [
@@ -211,6 +228,7 @@ export const lineOptions = {
211228
}
212229
};
213230

231+
214232
// Step
215233
export const stepOptions = Tools.merge({}, lineOptions, {
216234
title: "Step (discrete)",

Diff for: packages/core/demo/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ chartTypes.forEach(type => {
173173
break;
174174
case "line":
175175
case "line-time-series":
176+
case "line-time-series-customstroke":
176177
case "line-step":
177178
case "line-step-time-series":
178179
classToInitialize = LineChart;

Diff for: packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@carbon/charts",
3-
"version": "0.16.25",
3+
"version": "0.16.26",
44
"description": "Carbon charting components",
55
"main": "./bundle.js",
66
"module": "./index.js",

Diff for: packages/core/src/components/axes/axis.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AxisPositions, ScaleTypes, AxisTypes } from "../../interfaces";
44
import { Tools } from "../../tools";
55
import { ChartModel } from "../../model";
66
import { DOMUtils } from "../../services";
7+
const englishLocale = require("d3-time-format/locale/en-US.json");
78

89
// D3 Imports
910
import { scaleBand, scaleLinear, scaleTime, scaleLog, scaleOrdinal } from "d3-scale";
@@ -195,6 +196,8 @@ export class Axis extends Component {
195196
const timeLocale = Tools.getProperty(options, "locale", "time");
196197
if (timeLocale) {
197198
timeFormatDefaultLocale(timeLocale);
199+
} else {
200+
timeFormatDefaultLocale(englishLocale);
198201
}
199202
}
200203

Diff for: packages/core/src/components/graphs/scatter.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ export class Scatter extends Component {
5454
dotsEnter.merge(dots)
5555
.raise()
5656
.classed("dot", true)
57-
.classed("filled", d => this.model.getIsFilled(d.datasetLabel, d.label, d.value, filled))
58-
.classed("unfilled", d => !this.model.getIsFilled(d.datasetLabel, d.label, d.value, filled))
57+
.classed("filled", d => this.model.getIsFilled(d.datasetLabel, d.label, d.value, d, filled))
58+
.classed("unfilled", d => !this.model.getIsFilled(d.datasetLabel, d.label, d.value, d, filled))
5959
.attr("cx", (d, i) => this.services.axes.getXValue(d, i))
6060
.transition(this.services.transitions.getTransition("scatter-update-enter", animate))
6161
.attr("cy", (d, i) => this.services.axes.getYValue(d, i))
6262
.attr("r", options.points.radius)
6363
.attr("fill", d => {
64-
if (this.model.getIsFilled(d.datasetLabel, d.label, d.value, filled)) {
65-
return this.model.getFillColor(d.datasetLabel, d.label, d.value);
64+
if (this.model.getIsFilled(d.datasetLabel, d.label, d.value, d, filled)) {
65+
return this.model.getFillColor(d.datasetLabel, d.label, d.value, d);
6666
}
6767
})
6868
.attr("fill-opacity", filled ? 0.2 : 1)
69-
.attr("stroke", d => this.model.getStrokeColor(d.datasetLabel, d.label, d.value))
69+
.attr("stroke", d => this.model.getStrokeColor(d.datasetLabel, d.label, d.value, d))
7070
.attr("opacity", 1);
7171

7272
// Add event listeners to elements drawn

Diff for: packages/core/src/interfaces/charts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ export interface BaseChartOptions {
4141
/**
4242
* Optional function to determine whether is filled based on datasetLabel, label, and/or value
4343
*/
44-
getIsFilled?: (datasetLabel: any, label?: any, value?: any, defaultFilled?: boolean) => boolean;
44+
getIsFilled?: (datasetLabel: any, label?: any, value?: any, data?: any, defaultFilled?: boolean) => boolean;
4545
/**
4646
* Optional function to generate the fill color based on datasetLabel, label, and/or value
4747
*/
48-
getFillColor?: (datasetLabel: any, label?: any, value?: any, defaultFillColor?: string) => string;
48+
getFillColor?: (datasetLabel: any, label?: any, value?: any, data?: any, defaultFillColor?: string) => string;
4949
/**
5050
* Optional function to generate the stroke color based on datasetLabel, label, and/or value
5151
*/
52-
getStrokeColor?: (datasetLabel: any, label?: any, value?: any, defaultStrokeColor?: string) => string;
52+
getStrokeColor?: (datasetLabel: any, label?: any, value?: any, data?: any, defaultStrokeColor?: string) => string;
5353
/**
5454
* stylesheet options
5555
*/

Diff for: packages/core/src/model.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,30 @@ export class ChartModel {
193193
* @param value
194194
* @param defaultFilled the default for this chart
195195
*/
196-
getIsFilled(datasetLabel: any, label?: any, value?: any, defaultFilled?: boolean) {
196+
getIsFilled(datasetLabel: any, label?: any, value?: any, data?: any, defaultFilled?: boolean) {
197197
const options = this.getOptions();
198198
if (options.getIsFilled) {
199-
return options.getIsFilled(datasetLabel, label, value);
199+
return options.getIsFilled(datasetLabel, label, value, data, defaultFilled);
200200
} else {
201201
return defaultFilled;
202202
}
203203
}
204204

205-
getFillColor(datasetLabel: any, label?: any, value?: any) {
205+
getFillColor(datasetLabel: any, label?: any, value?: any, data?: any) {
206206
const options = this.getOptions();
207207
const defaultFillColor = this.getFillScale()[datasetLabel](label);
208208
if (options.getFillColor) {
209-
return options.getFillColor(datasetLabel, label, value, defaultFillColor);
209+
return options.getFillColor(datasetLabel, label, value, data, defaultFillColor);
210210
} else {
211211
return defaultFillColor;
212212
}
213213
}
214214

215-
getStrokeColor(datasetLabel: any, label?: any, value?: any) {
215+
getStrokeColor(datasetLabel: any, label?: any, value?: any, data?: any) {
216216
const options = this.getOptions();
217217
const defaultStrokeColor = this.colorScale[datasetLabel](label);
218218
if (options.getStrokeColor) {
219-
return options.getStrokeColor(datasetLabel, label, value, defaultStrokeColor);
219+
return options.getStrokeColor(datasetLabel, label, value, data, defaultStrokeColor);
220220
} else {
221221
return defaultStrokeColor;
222222
}

0 commit comments

Comments
 (0)