【发布时间】:2021-02-22 14:02:11
【问题描述】:
我想创建一个图表,其中图例根据来自 JSON 的不同 scenario 拆分为列数。图例项目应归类在每个标题下,但根据我所做的,标题总是被添加到每个图例项目之前。请指教。
这就是我所拥有的:
import React from "react";
import "./styles.css";
import * as Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import HC_more from "highcharts/highcharts-more";
import HC_exporting from "highcharts/modules/exporting";
import HC_series_label from "highcharts/modules/series-label";
import HC_boost from "highcharts/modules/boost";
import { map, slice } from "lodash";
HC_more(Highcharts);
HC_exporting(Highcharts);
HC_series_label(Highcharts);
HC_boost(Highcharts);
const App = (props) => {
const options = {
colors: [
"#800000",
"#9A6324",
"#808000",
"#469990",
"#000075",
"#e6194b",
"#f58231",
"#ffe119",
"#bfef45",
"#3cb44b",
"#42d4f4",
"#4363d8",
"#911eb4",
"#f032e6"
],
chart: {
zoomType: "x",
resetZoomButton: {
position: {
align: "left", // by default
verticalAlign: "top", // by default
x: -10,
y: 10
}
},
type: "line",
height: props.height ? props.height : `60%`
},
exporting: {
enabled: true,
chartOptions: {
xAxis: [
{
max: null
}
]
}
},
title: {
text: props.title
},
subtitle: {
text: ""
},
yAxis: {
title: {
text: null
},
labels: {
formatter: function () {
return `${Highcharts.numberFormat(
this.value / 1,
props.decimalPlaces,
"."
)}`;
},
style: {
fontSize: "13px"
}
}
},
legend: {
itemStyle: {
fontSize: "15px"
},
floating: false,
itemMarginBottom: 5,
width: 180,
itemWidth: 100,
useHTML: true,
labelFormatter: function () {
console.log(this);
console.log(`data.Series[Number(this.userOptions.id)]`);
return `<div>${
data.Series[Number(this.userOptions.id) - 1].scenario
}<div>${this.name}</div></div>`;
}
},
credits: {
enabled: false
},
xAxis: {
categories: data.Dates.map((item) => item.Date),
labels: {
style: {
fontSize: "13px"
}
}
},
plotOptions: {
series: {
boostThreshold: 2000,
label: {
enabled: false,
connectorAllowed: false
}
}
},
tooltip: {
pointFormatter: function () {
return `${Highcharts.numberFormat(
this.options.y / 1,
props.decimalPlaces,
"."
)}`;
}
},
series: map(slice(data.Series, 0, 15), (item) => {
return {
name: item.segment,
data: item.values,
type: "line",
id: item.id.toString()
};
})
};
return (
<HighchartsReact highcharts={Highcharts} options={options} {...props} />
);
};
export default App;
Stackblitz 链接:https://codesandbox.io/s/fragrant-cherry-2qhr6?file=/src/App.js:0-25119 https://stackblitz.com/edit/react-7owjmq
【问题讨论】:
-
恐怕无法使用标准 API 选项创建此图例,请参阅:github.com/highcharts/highcharts/issues/11309。实现此效果的可能解决方案是创建自定义图例。您可以在此处找到示例:highcharts.com/forum/viewtopic.php?t=32764 让我知道您对此有何看法。
-
@SebastianWędzel 这就是我所拥有的 - codesandbox.io/s/intelligent-fog-cds4u?file=/src/App.js 我创建了一个自定义 div 并将新的图例附加到其中。但是,当我尝试单击图例时,我想我错过了一点,因为最后一个总是切换。此外,如果您也可以帮助我渲染自定义图例中的符号,那将有很大帮助。
-
我看到你在这里打开了新问题:stackoverflow.com/questions/64811381/…,所以我认为你上面的问题已经过时了。
-
@SebastianWędzel 是的,这是正确的,因为它不包含自定义图例布局
标签: reactjs highcharts flexbox react-highcharts