@@ -11,7 +11,6 @@ import {
1111 getLabelPosition ,
1212 recalculateOverlappingLabels ,
1313 setTM ,
14- transformData ,
1514} from "../utils/streamgraph" ;
1615import {
1716 CHART_MARGIN ,
@@ -37,15 +36,6 @@ class Streamgraph extends React.Component {
3736 constructor ( props ) {
3837 super ( props ) ;
3938
40- this . stack = d3 . layout
41- . stack ( )
42- . offset ( "silhouette" )
43- . values ( ( d ) => d . values )
44- . x ( ( d ) => d . date )
45- . y ( ( d ) => d . value ) ;
46-
47- this . nest = d3 . nest ( ) . key ( ( d ) => d . key ) ;
48-
4939 this . labelPositions = [ ] ;
5040 }
5141
@@ -71,16 +61,18 @@ class Streamgraph extends React.Component {
7161 const { colors } = this . props ;
7262 const { width, height } = this . getDimensions ( ) ;
7363
74- const parsedData = JSON . parse ( this . props . data ) ;
75- const transformedData = transformData ( parsedData ) ;
76- const streams = this . getStreams ( transformedData ) ;
77-
7864 const xScale = d3 . time . scale ( ) . range ( [ 0 , width ] ) ;
7965 const yScale = d3 . scale . linear ( ) . range ( [ height , 0 ] ) ;
8066 const colorScale = d3 . scale . ordinal ( ) . range ( colors ) ;
8167
82- xScale . domain ( d3 . extent ( transformedData , ( d ) => d . date ) ) ;
83- yScale . domain ( [ 0 , d3 . max ( transformedData , ( d ) => d . y0 + d . y ) ] ) ;
68+ const streams = this . props . streams ;
69+ const streamEntries = streams . reduce (
70+ ( l , stream ) => [ ...l , ...stream . values ] ,
71+ [ ]
72+ ) ;
73+
74+ xScale . domain ( d3 . extent ( streamEntries , ( d ) => d . date ) ) ;
75+ yScale . domain ( [ 0 , d3 . max ( streamEntries , ( d ) => d . y0 + d . y ) ] ) ;
8476
8577 const area = d3 . svg
8678 . area ( )
@@ -94,7 +86,7 @@ class Streamgraph extends React.Component {
9486
9587 this . renderBackground ( container , width , height ) ;
9688 this . renderStreams ( container , streams , area , colorScale ) ;
97- this . renderAxes ( container , parsedData , xScale , yScale , width , height ) ;
89+ this . renderAxes ( container , streams , xScale , yScale , width , height ) ;
9890 this . renderLabels ( container , xScale , yScale , width ) ;
9991 this . renderTooltip ( container , xScale ) ;
10092 this . renderLineHelper ( container ) ;
@@ -159,19 +151,19 @@ class Streamgraph extends React.Component {
159151 * Renders the graph axes using d3.
160152 *
161153 * @param {Object } container the d3 representation of #streamgraph-chart
162- * @param {Object } parsedData the parsed JSON containing the streamgraph data
154+ * @param {Object } streams the stream array
163155 * @param {Function } xScale x coordinate scaling function
164156 * @param {Function } yScale y coordinate scaling function
165157 * @param {number } width graph width
166158 * @param {number } height graph height
167159 */
168- renderAxes ( container , parsedData , xScale , yScale , width , height ) {
160+ renderAxes ( container , streams , xScale , yScale , width , height ) {
169161 const xAxis = d3 . svg
170162 . axis ( )
171163 . scale ( xScale )
172164 . orient ( "bottom" )
173165 . tickFormat ( d3 . time . format ( "%Y" ) )
174- . ticks ( d3 . time . year , Math . ceil ( parsedData . x . length / MAX_TICKS_X ) ) ;
166+ . ticks ( d3 . time . year , Math . ceil ( streams . length / MAX_TICKS_X ) ) ;
175167
176168 const yAxis = d3 . svg
177169 . axis ( )
@@ -509,28 +501,10 @@ class Streamgraph extends React.Component {
509501
510502 return { width, height } ;
511503 }
512-
513- getStreams ( transformedData ) {
514- const nestedEntries = this . nest . entries ( transformedData ) ;
515- const streams = this . stack ( nestedEntries ) ;
516-
517- streams . forEach ( ( stream ) => {
518- const firstTransformedEntry = transformedData . find (
519- ( t ) => t . key === stream . key
520- ) ;
521- if ( ! firstTransformedEntry ) {
522- stream . docIds = [ ] ;
523- return ;
524- }
525- stream . docIds = firstTransformedEntry . docIds ;
526- } ) ;
527-
528- return streams ;
529- }
530504}
531505
532506const mapStateToProps = ( state ) => ( {
533- data : state . streamgraph . data ,
507+ streams : state . streamgraph . streams ,
534508 colors : state . streamgraph . colors ,
535509 width : state . chart . streamWidth ,
536510 height : state . chart . streamHeight ,
0 commit comments