-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shapes support for lines in D3 (#368)
* Add shapes support for lines in D3 * Fix dashStyle type * Add linecap option, fix story * Add legend dash style * Fix legend types * Fix story, default value and minor issues * Fix dashStyle passing from seriesOptions * Fix linecap passing from seriesOptions * Remove unneccessary story * Minor pr fixes
- Loading branch information
1 parent
3fbb47c
commit a3b952b
Showing
14 changed files
with
244 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export {CHARTKIT_SCROLLABLE_NODE_CLASSNAME} from './common'; | ||
|
||
export * from './widget-data'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export enum DashStyle { | ||
Dash = 'Dash', | ||
DashDot = 'DashDot', | ||
Dot = 'Dot', | ||
LongDash = 'LongDash', | ||
LongDashDot = 'LongDashDot', | ||
LongDashDotDot = 'LongDashDotDot', | ||
ShortDash = 'ShortDash', | ||
ShortDashDot = 'ShortDashDot', | ||
ShortDashDotDot = 'ShortDashDotDot', | ||
ShortDot = 'ShortDot', | ||
Solid = 'Solid', | ||
} | ||
|
||
export enum LineCap { | ||
Butt = 'butt', | ||
Round = 'round', | ||
Square = 'square', | ||
None = 'none', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react'; | ||
import {ChartKitWidgetData, LineSeriesData, LineSeries} from '../../../../types'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import nintendoGames from '../../examples/nintendoGames'; | ||
import {DashStyle} from '../../../../constants'; | ||
|
||
const SHAPES = { | ||
[DashStyle.Solid]: 1, | ||
[DashStyle.Dash]: 2, | ||
[DashStyle.Dot]: 3, | ||
[DashStyle.ShortDashDot]: 4, | ||
[DashStyle.LongDash]: 5, | ||
[DashStyle.LongDashDot]: 6, | ||
[DashStyle.ShortDot]: 7, | ||
[DashStyle.LongDashDotDot]: 8, | ||
[DashStyle.ShortDash]: 9, | ||
[DashStyle.DashDot]: 10, | ||
[DashStyle.ShortDashDotDot]: 11, | ||
}; | ||
|
||
const selectShapes = (): DashStyle[] => Object.values(DashStyle); | ||
const getShapesOrder = () => selectShapes().sort((a, b) => SHAPES[a] - SHAPES[b]); | ||
|
||
const SHAPES_IN_ORDER = getShapesOrder(); | ||
|
||
function prepareData(): ChartKitWidgetData { | ||
const games = nintendoGames.filter((d) => { | ||
return d.date && d.user_score; | ||
}); | ||
|
||
const byGenre = (genre: string) => { | ||
return games | ||
.filter((d) => d.genres.includes(genre)) | ||
.map((d) => { | ||
return { | ||
x: d.date, | ||
y: d.user_score, | ||
label: d.title, | ||
}; | ||
}) as LineSeriesData[]; | ||
}; | ||
|
||
return { | ||
series: { | ||
options: { | ||
line: { | ||
lineWidth: 2, | ||
}, | ||
}, | ||
data: [ | ||
{ | ||
name: '3D', | ||
type: 'line', | ||
data: byGenre('3D'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
{ | ||
name: '2D', | ||
type: 'line', | ||
data: byGenre('2D'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Strategy', | ||
type: 'line', | ||
data: byGenre('Strategy'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Shooter', | ||
type: 'line', | ||
data: byGenre('Shooter'), | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
xAxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Release date', | ||
}, | ||
}, | ||
yAxis: [ | ||
{ | ||
title: {text: 'User score'}, | ||
labels: { | ||
enabled: true, | ||
}, | ||
ticks: { | ||
pixelInterval: 120, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
export const LinesWithShapes = () => { | ||
const data = prepareData(); | ||
|
||
(data.series.data as LineSeries[]).forEach((graph, i) => { | ||
graph.dashStyle = SHAPES_IN_ORDER[i % SHAPES_IN_ORDER.length]; | ||
}); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
height: '80vh', | ||
width: '100%', | ||
}} | ||
> | ||
<ChartKit type="d3" data={data} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.