Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getColorScale to enable displaying multiple lines in LineChart #11

Merged
merged 26 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6dab9bb
Add sequential colors in theme
chenesan Feb 26, 2019
d760dd0
Make getRecordFieldSelector accept all Scale argument
chenesan Feb 26, 2019
a4d8664
Add getColorScale
chenesan Feb 26, 2019
ba64566
Add multiple color charts in LineChart
chenesan Feb 26, 2019
2417573
Add color scale on TooltipLayer and HoverIndicator
chenesan Feb 26, 2019
5d911a9
Refactor dataGroup logic
chenesan Feb 26, 2019
3a329c5
Extract DataLine component
chenesan Feb 26, 2019
415c7e0
Fix linter issue of TooltipLayer
chenesan Feb 26, 2019
43bec01
Fix linter issue of getColorScale
chenesan Feb 26, 2019
b5b09db
Fix linter issue of getDataGroup
chenesan Feb 26, 2019
ef76ce8
Fix linter issue of themes
chenesan Feb 26, 2019
5a8b112
Fix sorting issue of DataGroup
chenesan Feb 26, 2019
77245f7
Add quantitative example for multi lines
chenesan Feb 26, 2019
321eb57
Fix undefined type error of LineChart
chenesan Feb 27, 2019
ccebf77
Rename GetColorScaleArgs as ColorScaleArgs
chenesan Mar 4, 2019
dfed6f6
Use d3-array extent in getColorScale
chenesan Mar 4, 2019
b395275
Fix type definition of getColorScale args
chenesan Mar 4, 2019
d7eadb1
Add comment on getColorScale
chenesan Mar 4, 2019
4e47016
Rename getDataGroup as getDataGroupByFields
chenesan Mar 4, 2019
f2bfe00
Extract getDataGroupByEncodings
chenesan Mar 4, 2019
d81d659
Simplify getColrScaleSetting with lodash.map
chenesan Mar 4, 2019
5f7f4e5
Fix documentation of getdataGroupByEncodings
chenesan Mar 4, 2019
57bad27
Simplify getNumericDomain logic
chenesan Mar 5, 2019
c804601
Fix LineChart.tsx formatting
chenesan Mar 5, 2019
805c85f
Update CHANGELOG.md
chenesan Mar 5, 2019
c60681d
Merge branch 'develop' into feature/color-scale
chenesan Mar 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Add `getColorScale` to receive color encoding in charts. (#11)
- Extract the animation frame controls as a custom effect hook. (#10)
- Add `tslint-react-hooks` rules to lint React Hooks. (#8)
- Add `ThemeProvider` and color / xy axis themes config for customize theme. (#6)
Expand Down
51 changes: 51 additions & 0 deletions docs/charts/LineChart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ menu: Charts
import { Playground, PropsTable } from 'docz'
import { LineChart } from '@ichef/transcharts-chart'
import lineData from '../sampleData/lineData';
import multiLinesData from '../sampleData/multiLinesData'

# Line Chart

Expand Down Expand Up @@ -64,6 +65,56 @@ const lineData = [
</div>
</Playground>

## Multiple lines

### Nominal color field

<Playground>
<div style={{ width: '100%', height: '320px' }}>
<LineChart
data={multiLinesData}
x={{ type: 'quantitative', field: 'x' }}
y={{ type: 'quantitative', field: 'y' }}
color={{
field: "type",
type: "nominal",
}}
/>
</div>
</Playground>

### Quantitative color field

<Playground>
<div style={{ width: '100%', height: '320px' }}>
<LineChart
data={[
{ x: 1, y: 5, size: 3 },
{ x: 3, y: 6, size: 3 },
{ x: 2, y: 3, size: 3 },
{ x: 9, y: 7, size: 3 },
{ x: 7, y: 1, size: 3 },
{ x: 4, y: 2, size: 5 },
{ x: 1, y: 10, size: 5 },
{ x: 6, y: 4, size: 5 },
{ x: 8, y: 9, size: 5 },
{ x: 9, y: 0, size: 5 },
{ x: 5, y: 3, size: 8 },
{ x: 3, y: 8, size: 8 },
{ x: 4, y: 4, size: 8 },
{ x: 10, y:7, size: 8 },
]}
x={{ type: 'quantitative', field: 'x' }}
y={{ type: 'quantitative', field: 'y' }}
color={{
field: "size",
type: "quantitative",
}}
/>
</div>
</Playground>


## Props

<PropsTable of={LineChart} />
14 changes: 14 additions & 0 deletions docs/sampleData/multiLinesData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const mutliLinesData = [
{ x: 0, y: 9, type: "type1", date: '2019/01/21 00:00:00', weekday: 'Mon' },
{ x: 1, y: 5, type: "type2", date: '2019/01/22 00:00:00', weekday: 'Tue' },
{ x: 2, y: 5, type: "type2", date: '2019/01/23 00:00:00', weekday: 'Wed' },
{ x: 3, y: 3, type: "type1", date: '2019/01/24 00:00:00', weekday: 'Thu'},
{ x: 4, y: 1, type: "type2", date: '2019/01/25 00:00:00', weekday: 'Fri' },
{ x: 10, y: 9, type: "type1", date: '2019/01/21 00:00:00', weekday: 'Mon' },
{ x: 6, y: 5, type: "type2", date: '2019/01/22 00:00:00', weekday: 'Tue' },
{ x: 7, y: 5, type: "type2", date: '2019/01/23 00:00:00', weekday: 'Wed' },
{ x: 2, y: 3, type: "type1", date: '2019/01/24 00:00:00', weekday: 'Thu'},
{ x: 8, y: 1, type: "type2", date: '2019/01/25 00:00:00', weekday: 'Fri' },
];

export default mutliLinesData;
101 changes: 72 additions & 29 deletions packages/chart/src/line/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import {
TooltipLayer,
// from common types
Margin,
FieldSelector,
Encoding,
AxisEncoding,
ColorEncoding,
// from utils
getColorScale,
getDataGroupByEncodings,
getXAxisScale,
getYAxisScale,
// from themes
Expand All @@ -30,6 +35,7 @@ export interface LineChartProps {
data: object[];
x: AxisEncoding;
y: AxisEncoding;
color?: ColorEncoding;
/** Should show the axis on the left or not */
showLeftAxis: boolean;
/** Should show the axis on the bottom or not */
Expand Down Expand Up @@ -69,10 +75,45 @@ const HoveringIndicator: FunctionComponent<{
);
};

const DataLine: FunctionComponent<{
color: string,
xSelector: FieldSelector,
ySelector: FieldSelector,
rows: object[],
}> = ({ color, xSelector, ySelector, rows }) => {
const lineDots = rows.map((dataRow, index) => (
<circle
key={`c-${index}`}
cx={xSelector.getScaledVal(dataRow)}
cy={ySelector.getScaledVal(dataRow)}
r={3.5}
fill={color}
/>
));
return (
<>
{/* Draw the line */}
<LinePath
data={rows}
x={xSelector.getScaledVal}
y={ySelector.getScaledVal}
stroke={color}
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>

{/* Draw dots on the line */}
{lineDots}
</>
);
};

export const LineChart: FunctionComponent<LineChartProps> = ({
data,
x,
y,
color,
margin = {
top: 20,
right: 20,
Expand Down Expand Up @@ -104,20 +145,36 @@ export const LineChart: FunctionComponent<LineChartProps> = ({
const xSelector = xAxis.selector;
const ySelector = yAxis.selector;

const bandWidth = graphWidth / (data.length - 1);

/** Width of the collision detection rectangle */
const color = theme.colors.category[0];
const bandWidth = graphWidth / (data.length - 1);
const colorScale = (typeof color !== 'undefined') && getColorScale({
data,
encoding: color,
colors: theme.colors,
});
const defaultColor = theme.colors.category[0];
const getColor = colorScale
? colorScale.selector.getScaledVal
: () => defaultColor;
const sortedData = data.sort(
(rowA, rowB) => xSelector.getOriginalVal(rowA) - xSelector.getOriginalVal(rowB)
);
const encodings = [color].filter((encoding): encoding is Encoding => !!encoding);
const dataGroup = getDataGroupByEncodings(sortedData, encodings);

const lineDots = data.map((dataRow, index) => (
<circle
key={`c-${index}`}
cx={xSelector.getScaledVal(dataRow)}
cy={ySelector.getScaledVal(dataRow)}
r={3.5}
fill={color}
/>
));
const graphGroup = dataGroup.map(
rows => {
const colorString: string = getColor(rows[0]);
return (
<DataLine
color={colorString}
rows={rows}
xSelector={xSelector}
ySelector={ySelector}
/>
);
}
);

return (
<div
Expand All @@ -126,7 +183,6 @@ export const LineChart: FunctionComponent<LineChartProps> = ({
>
<svg width={outerWidth} height={outerHeight}>
<g transform={`translate(${margin.left}, ${margin.top})`}>
{/* Draw the axes */}
<AxisLayer
width={graphWidth}
height={graphHeight}
Expand All @@ -137,25 +193,13 @@ export const LineChart: FunctionComponent<LineChartProps> = ({
yAxis={yAxis}
/>

{/* Draw the line */}
<LinePath
data={data}
x={xSelector.getScaledVal}
y={ySelector.getScaledVal}
stroke={color}
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>

{/* Draw dots on the line */}
{lineDots}
{graphGroup}
<HoveringIndicator
hovering={hovering}
xPos={xSelector.getScaledVal(data[hoveredPoint.index])}
yPos={ySelector.getScaledVal(data[hoveredPoint.index])}
height={graphHeight}
color={color}
color={getColor(data[hoveredPoint.index])}
/>

{/* Areas which are used to detect mouse or touch interactions */}
Expand Down Expand Up @@ -190,7 +234,6 @@ export const LineChart: FunctionComponent<LineChartProps> = ({
/>
</g>
</svg>

{/* Draw the tooltip */}
<TooltipLayer
hovering={hovering}
Expand All @@ -201,7 +244,7 @@ export const LineChart: FunctionComponent<LineChartProps> = ({
margin={margin}
xSelector={xSelector}
ySelector={ySelector}
color={color}
getColor={getColor}
/>
</div>
);
Expand Down
13 changes: 11 additions & 2 deletions packages/graph/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Scale {
/**
* Range of input channel
*/
range: [any, any];
range?: ReadonlyArray<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of ReadonlyArray 👍


/** Returns the formatted value according to the type of the axis */
getValue: (val: any) => any;
Expand All @@ -55,10 +55,13 @@ export interface AxisScale extends Scale {
* Range of the axis: [min, max]
* it should match the inner width and height of the graph
*/
range: [number, number];
range?: [number, number];
}

export type EncodingDataType = 'nominal' | 'ordinal' | 'quantitative' | 'temporal';
export interface ColorScale extends Scale {
range?: [string, string] | ReadonlyArray<string>
}

export interface Encoding {
field: string;
Expand All @@ -70,6 +73,8 @@ export interface AxisEncoding extends Encoding {
scale?: AxisScaleType;
}

export type ColorEncoding = Encoding;

export interface Margin {
top: number;
right: number;
Expand All @@ -93,6 +98,10 @@ export interface Theme {
colors: {
/** colors used for nominal data */
category: ReadonlyArray<string>;
sequential: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scheme: ReadonlyArray<string>;
interpolator: (val: number) => string;
}
};
/** x-axis theme config */
xAxis: AxisTheme;
Expand Down
2 changes: 2 additions & 0 deletions packages/graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export * from './themes';
export * from './tooltip/Tooltip';
export * from './tooltip/TooltipItem';
export * from './utils/getAxisScale';
export * from './utils/getColorScale';
export * from './utils/getDataGroupByEncodings';
export * from './utils/getRecordFieldSelector';
10 changes: 6 additions & 4 deletions packages/graph/src/layers/TooltipLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface TooltipLayerProps {
margin: Margin;
xSelector: FieldSelector;
ySelector: FieldSelector;
color: string;
getColor: FieldSelector['getScaledVal'];
}

/** Generates the tooltip box */
Expand All @@ -26,7 +26,7 @@ export const TooltipLayer: React.FC<TooltipLayerProps> = ({
margin,
xSelector,
ySelector,
color,
getColor,
}) => {
const { index, position } = hoveredPoint;

Expand All @@ -42,8 +42,10 @@ export const TooltipLayer: React.FC<TooltipLayerProps> = ({
show={hovering}
>
<h3>{xSelector.getFormattedStringVal(data[index])}</h3>
{/* TODO: unify the way of dealing colors of the fields */}
<TooltipItem color={color} text={ySelector.getFormattedStringVal(data[index])} />
<TooltipItem
color={getColor(data[index])}
text={ySelector.getFormattedStringVal(data[index])}
/>
</Tooltip>
);
};
6 changes: 5 additions & 1 deletion packages/graph/src/themes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { schemeCategory10 } from 'd3-scale-chromatic';
import { schemeCategory10, interpolateBlues, schemeBlues } from 'd3-scale-chromatic';
import deepmerge from 'deepmerge';

import { Theme } from '../common/types';
Expand All @@ -14,6 +14,10 @@ export const themes = {
default: {
colors: {
category: schemeCategory10,
sequential: {
scheme: schemeBlues[9],
interpolator: interpolateBlues,
},
},
xAxis: {
strokeColor: '#7c8a94',
Expand Down
Loading