Echarts, как вы добавляете пунктирную вертикальную линию между конкретными барами на гистограмме? - PullRequest
1 голос
/ 31 мая 2019

Я работаю с Echarts и создал гистограмму.Я пытаюсь добавить две вертикальные пунктирные линии для разделения Source3 и Source4 и еще одну пунктирную вертикальную линию, чтобы показать разделение SourceSix и SourceSeven.Я попытался возиться с markLine в столбце и добавить line в качестве части данных после столбца, но я, кажется, могу понять это.

Код:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- including ECharts file -->
    <script src="echarts.js"></script>
</head>
<body>
<!-- prepare a DOM container with width and height -->
<div id="main" style="width: 1600px;height:800px;"></div>
<script type="text/javascript">
    // based on prepared DOM, initialize echarts instance
    var myChart = echarts.init(document.getElementById('main'));

    // specify chart configuration item and data
    var option = {
        title: {
            text: 'Counts by Intel Source'
        },

        legend: {
            data:['Count']

        },
        xAxis: {
            type: 'category',
            data: ['SourceOne','SourceTwo','SourceThree','SourceFour','SourceFive','SourceSix','SourceSeven','SourceEight'],
        axisLabel: {
          fontWeight: 'bold',
          fontSize: 16,
          margin: 1,
            }
        },
        yAxis: {
          type: 'log',
          axisLabel: {
            fontWeight: 'bold',
          }
        },

        labelLine: {
     lineStyle: {
                 color: 'rgba(255, 255, 255, 0.3)'
             }
        },

    series: [
      {
          name: 'SourceOne',
          type: 'bar',
          stack: 'Chart 1',
          color: '#ed2d2e',
          data: [1819931,,,,,,,],
          },
        {
            name: 'SourceTwo',
            type: 'bar',
            stack: 'Chart 1',
            color: '#0bb5b5',
            data: [,1291396,,,,,,]
        },
        {
            name: 'SourceThree',
            type: 'bar',
            stack: 'Chart 1',
            color: '#662c91',
            data: [,,161,,,,,]
        },
        {
        name: 'SourceFour',
        type: 'bar',
        stack: 'Chart 1',
        color: '#0e107b',
        data: [,,,133279,,,,]
    },

    {
        name: 'SourceFive',
        type: 'bar',
        stack: 'Chart 1',
        color: '#a11d20',
        data: [,,,,1275,,,]
    },

    {
        name: 'SourceSix',
        type: 'bar',
        stack: 'Chart 1',
        color: '#f37d22',
         data: [,,,,,119,,]
    },
    {
        name: 'SourceSeven',
        type: 'bar',
        stack: 'Chart 1',
        color: '#008c47',
        data: [,,,,,,25224,]
    },
    {
        name: 'SourceEight',
        type: 'bar',
        stack: 'Chart 1',
        color: '#1859a9',
        data: [,,,,,,,6798]
    },
]
};

    // use configuration item and data specified to show chart
    myChart.setOption(option);
</script>

Желаемый выход:

enter image description here

1 Ответ

2 голосов
/ 17 июня 2019

Мне удалось добиться эффекта, аналогичного тому, который вы ищете, используя следующую опцию: я преобразовал вашу ось х в ось «значения», чтобы мы могли поместить маркерные линии на определенные места между вашей серией. При этом вам нужно назначить индекс оси x для каждой из ваших точек данных, но он также позволяет вам добавить дополнительную точку к sourceThree и sourceSix, которые позволят вам нарисовать линию между ними в виде типа: ' средняя 'markLine:

option = {
   title : {
        text: 'Chart Title',
        subtext: 'subtitle'
    },
    tooltip : {
        trigger: 'axis',
        axisPointer:{
            show: true,
            type : 'cross',
            lineStyle: {
                type : 'dashed',
                width : 1
            }
        },
        formatter : function (params) {
            return params.seriesName + ' : [ '
                   + params.value[0] + ', ' 
                   + params.value[1] + ' ]';
        }
    },
    xAxis : [
        {
            type : 'value'
        }
    ],
    yAxis : [
        {
            type : 'log',
            axisLine: {
                lineStyle: {
                    color: '#dc143c'
                }
            }
        }
    ],
    series : [
        {
            name:' SourceOne',
            type:'bar',
            data:[
               [1, 1819931]
            ]
        },
      {
            name:' SourceTwo',
            type:'bar',
            data:[
               [2, 1291396]
            ]
        },
            {
            name:' SourceThree',
            type:'bar',
            data:[
               [3, 161], [4, 0]
            ],
            markLine : {
              silent: true, // ignore mouse events
              data : [
                // Horizontal Axis (requires valueIndex = 0)
                {type : 'average', name: 'Marker Line', valueIndex: 0, itemStyle:{normal:{color:'#1e90ff'}}},
              ]
            }
        },
              {
            name:' SourceFour',
            type:'bar',
            data:[
               [4, 133279]
            ]
        },
      {
            name:' SourceFive',
            type:'bar',
            data:[
               [5, 1275]
            ]
        },
            {
            name:' SourceSix',
            type:'bar',
            data:[
               [6, 119], [7, 0] // this extra data point is the hack to get your marker line in between your series
            ],
            markLine : {
              silent: true, // ignore mouse events
              label: {show: false},
              data : [
                // Horizontal Axis (requires valueIndex = 0)
                {type: 'average', name: 'Line Marker', valueIndex: 0},
              ]
            }
        },
      {
            name:' SourceSeven',
            type:'bar',
            data:[
               [7, 25224]
            ]
        },
      {
            name:' SourceEight',
            type:'bar',
            data:[
               [8, 6798]
            ]
        }
    ]
};

Это должно привести к следующей таблице, и вы можете попробовать ее самостоятельно, вставив эту опцию здесь: https://echarts.baidu.com/echarts2/doc/example/bar13.html#

Chart fiddle

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...