现在在使用echarts作图,拿到后端的id值以后,前端页面的图例不显示颜色,在网上查询以后说是数字的原因,需要转为字符串
代码如下
<script type="text/javascript">
window.onload = function(){
initLine1();
function initLine1() {
var mychart = echarts.init(document.getElementById("chart"));
var series_data = [];
var legends = []; // 添加一个存储所有图例的数组
{% for i in obj %}
series_data.push([]);
legends.push({{ i.id}}); // 将每个id值添加到图例数组中
{% endfor %}
console.log(series_data);
{% for i in obj %}
var n = Number({{forloop.counter}} - 1)
series_data[n].push({{ i.r400 }});
series_data[n].push({{ i.r401 }});
{% endfor %}
var s_data = []
series_data.forEach((v, i) => {
var obj = {
data: v,
type: "line",
symbol:'none',
name: legends[i] // 将每个系列的name设置为对应的id值
};
s_data.push(obj);
});
var option = {
xAxis: {
name:'波长 Wavelength(nm)',
nameLocation:'middle',
nameGap:30,
nameTextStyle:{fontSize:15},
axisLabel:{
interval:99
},
type: "category",
data:["400","401"]
},
yAxis: {
name:'反射率 Reflectance',
nameLocation:'middle',
nameGap:30,
nameTextStyle:{fontSize:15},
axisLine:{
show:true,
},
type: "value"
},
tooltip: { // 鼠标悬浮提示框显示 X和Y 轴数据
trigger: 'axis',
backgroundColor: 'rgba(32, 33, 36,.7)',
borderColor: 'rgba(32, 33, 36,0.20)',
borderWidth: 1,
textStyle: { // 文字提示样式
color: '#fff',
fontSize: '12'
},
axisPointer: { // 坐标轴虚线
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
},
},
toolbox: {
feature: {
saveAsImage: {}
}
},
legend: { // 添加legend组件
data: legends // 将图例数组设置为legend的data属性的值
},
series: s_data
};
mychart.setOption(option);
};
}
</script>
目前效果如下
如有帮助必定采纳!