I'm creating charts using react-chartist. On labels I want to add image next to label text. I tried all of my ideas, but none work. Everything return [object Object]. Whether there is a good solution of this issue? I can also add labels using just css, but if I can do it inside <Chartist /> component it would be much easier.
Simple code example with my attempts (codesandbox demo below):
class Chart extends React.Component {
render() {
const labels = ["label 1", "label 2", "label 3"];
const images = [
"http://placekitten.com/100/100",
"http://placekitten.com/101/101",
"http://placekitten.com/102/102"
];
const series = [[40, 30, 20]];
const labelsInsideReactFragment = labels.map((el, key) => (
<React.Fragment>
<img src={images[key]} /> {el}
</React.Fragment>
));
const labelsViaGhIssue = labels.map((el, key) => {
return {
label: el,
image: images[key]
};
});
const labelsInsideDiv = labels.map((el, key) => (
<div>
<img src={images[key]} /> {el}
</div>
));
const labelsOnlyImg = labels.map((el, key) => <img src={images[key]} />);
const data1 = {
labels,
series
};
const data2 = {
labels: labelsViaGhIssue,
series
};
const data3 = {
labels: labelsInsideDiv,
series
};
const data4 = {
labels: labelsInsideReactFragment,
series
};
const data5 = {
labels: labelsOnlyImg,
series
};
const options = {
height: "200px",
width: "500px"
};
return (
<div>
<ChartistGraph data={data1} options={options} type="Bar" />
<ChartistGraph data={data2} options={options} type="Bar" />
<ChartistGraph data={data3} options={options} type="Bar" />
<ChartistGraph data={data4} options={options} type="Bar" />
<ChartistGraph data={data5} options={options} type="Bar" />
</div>
);
}
}
Demo: https://codesandbox.io/s/chartist-8oeju
I also find this demo on jsbin: https://jsbin.com/gulokelide/edit?js,output
but it didn't work in React I guess.
The JSbin example uses a custom
on("draw")function which isn't supported byreact-chartist(and it's also manually calculating the label placement, so it won't scale if the width or the amount of data in the series changes). That said, the simplest solution would be to make your ownChartistinstance and alter it to suit your needs.Working example (labels and images will scale to the center regardless of the width and/or the number of items within the
series):Demo
Source
components/Chart