Sort and index multi-series data correctly in line chart

- Sorting must be done on copies to preserve orders.
 - Indices should be reversed since rendering is also reversed (the back
   layer (the tallest) is rendered first to create the stacking effect).
This commit is contained in:
Michael Lange
2021-03-15 19:45:39 -07:00
parent 8ae76455fb
commit 8ab301408f
2 changed files with 5 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
<div data-test-chart-tooltip class="chart-tooltip {{if @active "active" "inactive"}}" style={{@style}} ...attributes>
<ol>
{{#each @data as |props|}}
{{yield props.series props.datum (inc props.index)}}
{{yield props.series props.datum (inc props.index)}}
{{/each}}
</ol>
</div>

View File

@@ -297,14 +297,14 @@ export default class LineChart extends Component {
formattedY: this.yFormat()(datum[yProp]),
datum,
},
index: seriesIndex,
index: data.length - seriesIndex - 1,
};
});
// Of the selected data, determine which is closest
const closestDatum = activeData.sort(
(a, b) => Math.abs(a.datum.datum[xProp] - x) - Math.abs(b.datum.datum[xProp] - x)
)[0];
const closestDatum = activeData
.slice()
.sort((a, b) => Math.abs(a.datum.datum[xProp] - x) - Math.abs(b.datum.datum[xProp] - x))[0];
// If any other selected data are beyond a distance threshold, drop them from the list
// xScale is used here to measure distance in screen-space rather than data-space.