Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angular - How to manually apply emphasis on series in echarts bar-graph?

I have a echarts bar graph with series of data. When a bar is clicked, emphasis is applied on it. Is there a way to programmatically select one of the series in the bar-graph to apply emphasis on when the graph loads?

Before Emphasis Emphasis when clicked

The options code I have for the bar graph:

  options = {
    markPoint: { data: ['Q1'] },
    color: ['#3398DB'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: ['Q1', 'Q2', 'Q3', 'Q4'],
        axisTick: {
          alignWithLabel: true,
        }
      }
    ],
    yAxis: [{
      type: 'value'
    }],
    series: [{
      name: 'Counters',
      type: 'bar',
      barWidth: '50%',
      data: [3,8,12,5],
      itemStyle: {
        //highlight
        emphasis: {
          barBorderColor: 'red',
          barBorderWidth: 2
        }

      }
    }]
  };

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, it's easy. The Echarts API has two methods with opposite actions:

A small example:

  var myChart = echarts.init(document.getElementById('main'));
    var chartData = [5, 20, 36, 10, 10, 20];

  var option = {
    tooltip: {},
    legend: {
      data: ['Label']
    },
    xAxis: {
      data: ["Category1", "Category2", "Category3", "Category4", "Category5", "Category6"]
    },
    yAxis: {},
    series: [{
      name: 'Series',
      type: 'bar',
      data: chartData,
            emphasis: {
                itemStyle: {
                    color: 'blue'
                }
            },
    }]
  };

  myChart.setOption(option);
    
    // Current selected dataPoint
    var selectedDataPoint = null;
    
    // Each eteration set another type of 
    setInterval(() => {
        var randomDataPoint = Math.floor(Math.random() * Math.floor(chartData.length));
        myChart.dispatchAction({ type: 'highlight', dataIndex: randomDataPoint })
    }, 800)
    
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...