This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from metasfresh/dev-608
Chart animations #608
- Loading branch information
Showing
5 changed files
with
167 additions
and
56 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,30 +1,97 @@ | ||
const mapDataset = (dataset, labelField) => Object.keys(dataset) | ||
import * as d3 from 'd3'; | ||
|
||
const mapDataset = (dataset, prevData, labelField) => Object.keys(dataset) | ||
.filter(key => key[0] !== '_' && key !== labelField) | ||
.map(key => ({ | ||
key, | ||
value: dataset[key] | ||
value: dataset[key], | ||
valuePrev: prevData && prevData[key] ? prevData[key] : 0 | ||
})); | ||
|
||
export const drawData = (svg, dimensions, ranges, data, labelField) => { | ||
export const isYRangeChanged = (data, prevData, fields) => { | ||
const keys = fields.map(field => field.fieldName); | ||
|
||
const yprev = prevData && d3.max(prevData, d => { | ||
return d3.max(keys, key => { | ||
return d[key]; | ||
}); | ||
}); | ||
|
||
const ynext = d3.max(data, d => { | ||
return d3.max(keys, key => { | ||
return d[key]; | ||
}); | ||
}); | ||
|
||
return yprev !== ynext; | ||
} | ||
|
||
export const isXRangeChanged = (data, prevData, svg) => { | ||
|
||
if(data.length!==(prevData && prevData.length)){ | ||
svg.select('g.datasets').selectAll('g').remove(); | ||
} else { | ||
data.map((item, index) => { | ||
if (prevData && JSON.stringify(Object.keys(data[index])) !== | ||
JSON.stringify(Object.keys(prevData[index])) ) { | ||
svg.select('g.datasets').selectAll('g').remove(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export const drawData = ( | ||
svg, dimensions, ranges, data, labelField, prev, fields, reRender) => { | ||
|
||
if(reRender){ | ||
svg.select('g.datasets').selectAll('g').remove(); | ||
} | ||
|
||
let chartData = []; | ||
data.map((item, index) => { | ||
chartData.push({data: item, prevData: prev ? prev[index] : 0}); | ||
}); | ||
|
||
const yChanged = isYRangeChanged(data, prev, fields); | ||
isXRangeChanged(data, prev, svg); | ||
|
||
const groups = svg.select('g.datasets') | ||
.selectAll('g') | ||
.data(data); | ||
.data(chartData); | ||
|
||
const bars = groups.enter().append('g') | ||
.classed('bar-group', true) | ||
.merge(groups) | ||
.attr('transform', d => 'translate(' + | ||
ranges.x0(d[labelField]) + ', 0)') | ||
ranges.x0(d.data[labelField]) + ', 0)') | ||
.selectAll('rect') | ||
.data(d => mapDataset(d, labelField)) | ||
.data(d => mapDataset(d.data, d.prevData, labelField)) | ||
|
||
bars.enter().append('rect') | ||
.classed('bar', true) | ||
.merge(bars) | ||
.attr('x', d => ranges.x1(d.key)) | ||
.attr('width', ranges.x1.bandwidth()) | ||
|
||
.attr('y', d => { | ||
|
||
if(yChanged || reRender){ | ||
return dimensions.height; | ||
} else { | ||
return ranges.y(d.valuePrev); | ||
} | ||
}) | ||
.attr('height', d => { | ||
|
||
if(yChanged || reRender){ | ||
return 0; | ||
} else { | ||
return dimensions.height - ranges.y(d.valuePrev); | ||
} | ||
}) | ||
.transition() | ||
.duration(1000) | ||
.attr('y', d => ranges.y(d.value)) | ||
.attr('height', d => dimensions.height - ranges.y(d.value)) | ||
.attr('fill', d => ranges.z(d.key)) | ||
.transition() | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as d3 from 'd3'; | ||
|
||
export const getSvg = (chartClass, width, height, wrapperWidth) => { | ||
const svg = d3.select('.' + chartClass) | ||
.attr('width', width) | ||
.attr('height', height) | ||
.append('g'); | ||
|
||
const chart = svg | ||
.attr('width', width) | ||
.attr('height', height) | ||
.append('g') | ||
.attr('class', 'chart') | ||
.attr('transform', | ||
'translate(' + wrapperWidth / 2 + ',' + 0.66*height + ')' | ||
); | ||
|
||
chart.append('g') | ||
.attr('class', 'slices'); | ||
|
||
return svg; | ||
}; |