ChartJS - 忽略标签

时间:2018-01-17 19:11:47

标签: charts chart.js

我正在使用ChartJS并需要这样的图表:https://tppr.me/OE08p 这意味着它应该忽略4个标签(灵活性,外部,稳定性,内部)并连接其他4个标签上的点(如屏幕截图中的红线所示)。

我可以在某种程度上以数据方式忽略这4个标签,但保留它们吗?

如果在chartjs中不可能,欢迎使用其他图表包/解决方案。

1 个答案:

答案 0 :(得分:1)

您可以使用 highchart.js 库,请参阅:

使用以下选项:

plotOptions: {
    series: {
        connectNulls: true
    }
}

并使用下面的地图函数过滤数据(例如):

data.map(filter)

<omissis>

function filter(item, index) {
    if (index==2)
        return null;
    else
        return item;
}

这是一个显示这种方法的jsfiddle:http://jsfiddle.net/beaver71/w6ozog1c/

或此处的摘录:

&#13;
&#13;
// original data
var data1 = [43000, 19000, 60000, 35000, 17000, 10000],
 data2 = [50000, 39000, 42000, 31000, 26000, 14000];

var chart = Highcharts.chart('container', {
    chart: {
        polar: true,
        type: 'line'
    },
    title: {
        text: 'Budget vs spending',
        x: -80
    },
    pane: {
        size: '80%'
    },
    xAxis: {
        categories: ['Sales', 'Marketing', 'Development', 'Customer Support',
                'Information Technology', 'Administration'],
        tickmarkPlacement: 'on',
        lineWidth: 0
    },
    yAxis: {
        gridLineInterpolation: 'polygon',
        lineWidth: 0,
        min: 0
    },
    tooltip: {
        shared: true,
        pointFormat: '<span style="color:{series.color}">{series.name}: <b>${point.y:,.0f}</b><br/>'
    },
    legend: {
        align: 'right',
        verticalAlign: 'top',
        y: 70,
        layout: 'vertical'
    },
    series: [{
        name: 'Allocated Budget',
        data: data1.map(filter),  // filtered data
        pointPlacement: 'on',
        color: 'red'
    }, {
        name: 'Actual Spending',
        data: data2,
        pointPlacement: 'on',
        color: 'green'
    }],
    plotOptions: {
        series: {
            lineWidth: 2,
            connectNulls: true	// connects also null value (bypassing)
        }
    }
});

var filterOn = true;

$('#button').click(function () {
	filterOn = !filterOn;
	if (filterOn)
    chart.series[0].setData(data1.map(filter));
  else
  	chart.series[0].setData(data1);
});

// filter function with your criteria
function filter(item, index) {
    if (index==2)
    	return null;
    else
    	return item;
}
&#13;
.highcharts-grid-line {
  stroke-width: 2;
}
&#13;
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<button id="button">Toggle filter (ignoring a point in red serie)</button>
<div id="container" style="min-width: 400px; max-width: 600px; height: 400px; margin: 0 auto"></div>
&#13;
&#13;
&#13;

相关问题