1

我正在创建标签冲突的 RGraph 饼图。虽然我使用labels.sticks属性来解决这个问题,但它对输出没有影响。

这是我绘制饼图的代码:

<script type="text/javascript">
$(document).ready(function(){
        // Some data that is to be shown on the pie chart. It should be an array of numbers.
        var data = [6.3, 2.1, 1.1, 3.2, 7.4, 10.5, 5.3, 27.4, 1.1, 4.2];
        var labels = ['Data Loggers 6', 'Data Translation   2', 'Energy Loggers 1', 'Hobo   3', 'iButton    7', 'ICP    10', 'MCC   5', 'Monnit 26', 'Orchestrator  1', 'Sensors    4'];

        for(var i = 0; i < data.length; i++)
        {
            labels[i] = labels[i] + ', ' + data[i] + '%';
        }

        var colors_arr = new Array('#00FFFF', '#7FFFD4', '#FFE4C4', '#0000FF', '#8A2BE2', '#A52A2A', '#7FFF00', '#FF7F50', '#B8860B', '#A9A9A9', '#8B008B', '#FF1493', '#228B22', '#DAA520', '#20B2AA', '#87CEFA', '#6B8E23', '#FF0000', '#FFFF00', '#F5DEB3');
        var colors = new Array();

        for(var i = 0; i < data.length; i++)
        {
            colors[i] = colors_arr[i];
        }

        // Create the Pie chart
        var pie = new RGraph.Pie({
            id: 'report_prospects_qty_products_canvas' ,
            data: data,
            options: {
                labels: {
                    self: labels,
                    sticks: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
                },
                tooltips: {
                    self: labels,
                    //event: 'onmousemove',
                },
                shadow: false,
                strokestyle: 'transparent',
                title: {
                    self: 'Products',
                    bold: false,
                    y: 10
                },
                radius: 70,
                colors: colors,
            text: {
                size: 8,
                color: "red",
                angle: 45
            },

            },
        }).roundRobin();

        $('#report_prospects_qty_products_wrapper').mouseout(function(){
            // Hide the tooltip
            RGraph.hideTooltip();
            // Redraw the canvas so that any highlighting is gone
            RGraph.redraw();
        });
});
</script>

HTML

<!-- Put this where you want the chart to show up: -->
<div id="cvs_wrapper">
    <!-- An example of using CSS to resize the canvas tag. -->
    <canvas id="report_prospects_qty_products_canvas" width="600" height="250" style="width:100%;">[No canvas support]</canvas>
</div>

输出:

在此处输入图像描述

为了解决标签冲突问题,我使用以下选项:

            options: {
                labels: {
                    self: labels,
                    sticks: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
                },

根据 RGraph 文档:

labels.sticks 规定显示标签的棒。这也可以是一系列棒长度 - 如果您遇到标签冲突,这可能很有用。默认值:假

仅供参考,我正在使用// version: 2015-05-24

4

1 回答 1

0

从您发布的配置片段来看,我猜您可能使用的是旧版本。当前版本(此时为 5.0)使用了更好的标签排列方式,因此不会发生冲突(除非您有大量标签)。

有一个演示页面很好地展示了这个新方法:

https://www.rgraph.net/demos/pie-basic.html

并且此代码与您可能习惯的代码没有什么不同:

labels = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];

new RGraph.Pie({
    id: 'cvs',
    data: [20,1,1,1,1,1,1],
    options: {
        tooltips: labels,
        labels: labels,
        shadow: false,
        colorsStroke: 'rgba(0,0,0,0)',
        exploded: 0
    }
}).roundRobin();
于 2019-05-13T08:40:49.580 回答