How can I incorporate a dashed vertical line in between select bars on a bar chart using Echarts?

I am currently utilizing Echarts and have successfully developed a bar chart. My goal is to incorporate two vertical dashed lines to distinguish between Source3 and Source4, as well as another dashed line to indicate the separation of SourceSix and SourceSeven. I have attempted various approaches with the markLine function in conjunction with a bar, but I am encountering issues figuring it out.

The current code snippet is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- including ECharts file -->
    <script src="echarts.js"></script>
</head>
<body>
<div id="main" style="width: 1600px;height:800px;"></div>
<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('main'));

    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]
            },
        ]
    };

    myChart.setOption(option);
</script>

The desired output can be viewed here: https://i.sstatic.net/oMJAc.png

Answer №1

I have successfully achieved a similar outcome to the effect you desire by implementing the following approach: I've transformed your x-axis into a 'value' x-axis, enabling us to insert markerLines at specific points within your series. This method involves assigning an x-axis index to each of your data points, allowing for the addition of extra points to sourceThree and sourceSix, facilitating the drawing of a line between them using a type:"average" 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]
            ]
        }
    ]
};

This configuration will generate the depicted chart, which you can experiment with yourself by inserting that option here:

https://i.sstatic.net/aHw68.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for extracting function data and storing it within scope after resolving a route in AngularJS

Is there a way to fetch data from a function and store it in the scope using resolve in an AngularJS route? For instance: .state('list.employee_list', { url: "/employee_list", templateUrl: "views/list/employee_list.html" ...

Establish the condition if the array is present

I am looking to conditionally set a state based on the existence of data retrieved from the server. const [names, setNames] = useState([...details?.names]) The details object is passed in as props and fetched from the database. The array details.names com ...

Preserve object properties while allowing for changes to be made without affecting

I am in need of transferring specific properties from one object to another while ensuring that the original object remains mutable. Here is the original object: var sourceObject = { "key1": "value1", "key2": "value2 ...

Node.js Buffer problem: How to tackle it

Encountering an issue with buffers in Node.js The constant buffer is defined as follows: var commands = { BufferOne : new Buffer([0XCA, 0X11, 0X00, 0X00, 0X60, 0X01, 0X00, 0X01, 0X08, 0X07, 0X6D, 0X00, 0X00, 0X00, 0X00, 0 ...

Encountering the "Uncaught TypeError: Cannot read properties of undefined (reading 'map')" error message while implementing Chart.js within a React Axios function

Struggling with creating a Chartjs chart in React using Axios post method to retrieve data from a Nodejs server. However, encountering the following error: "Uncaught TypeError: Cannot read properties of undefined (reading 'map')" Here's a s ...

What could be causing my jQuery to malfunction after I include the .sqrt() function?

Seeking assistance in creating a grid of divs, I am working on finding the square root of the user's input to determine the height and width required to form a square. The code below is what I currently have: $('#size').click(function(){ $ ...

Extracting and transforming an array into a list with the desired outcome

Looking for a way to flatten the array below into a single line array using Typescript/JavaScript? Student: any = [ { "id": "1", "name": "Jhon", "Marks": { "Math": "90", "English": "80", "Science": "70" } }, { "id": "2", "name": "Peter", "Marks": { "M ...

What is causing the search feature to malfunction on the Detail page?

In my project, I have different components for handling shows. The Shows.jsx component is responsible for rendering all the shows, while the ProductDetails component displays information about a single show. Additionally, there is a Search component which ...

Storing the locations of draggable items with personalized user-created material

If I needed to store dynamically created content in a specific position within a container, what would be the best approach? For instance, if a user enters a YouTube URL into an input box and it generates an embedded video in a box container upon hitting ...

Why are @Inject and Injectable important in Angular's Dependency Injection system?

constructor(private smartphoneService: smartphoneService) { } Although I am able to execute the code above without encountering any errors, I find myself pondering on the necessity of using @Inject and Injectable on services, Pipes, and other components. ...

Calculate the sum of the products when multiplying two values from every array of objects, using Reactjs/Javascript

I'm currently developing an eCommerce application and need to calculate the total price of items that users have ordered. I have an array named 'orders' which contains all the ordered items, each item has two keys - payablePrice and purchase ...

"Enhance your website with the dynamic duo of Dropzone

Currently, I am utilizing dropzone.js and loading it through ajax. I have assigned my menu ID as "#menu". The uploaded file should display in "#div1". Unfortunately, the callback function is not functioning properly. In an attempt to troubleshoot, I repl ...

I desire to perform a specific task when there is a modification in the path using react router framework

Though I am mindful of it. props.history.listen((location, action) => { console.log("when route changes",location); }) However, I need to implement it in a slightly different way. For instance, let's cons ...

Iterating through a collection of objects, triggering a promise for each object and recording its completion

I have encountered a challenge where I need to iterate through an array of objects obtained from a promise, and for each object in the array, I must invoke another promise. After all these promises are executed, I want to display "DONE" on the console. Is ...

Can the break statement be used in jQuery or JavaScript?

I created a function that picks a text based on the input string. If there is a match, it sets it as selected. Here is the function: function chooseDropdownText(dropdownId,selectedValue,hfId){ $('#'+dropdownId+' option').ea ...

Tips for creating a phone number placeholder that stays in place on a form

I'm looking to display the phone number format as a placeholder in the input field, without it disappearing as the user types. The standard HTML placeholder disappears as the user starts typing. I want the placeholder to remain visible and guide the ...

Unable to conceal the innerHTML once the error has been rectified

My error messages are displayed using innerHTML. How can I make them disappear once the error is corrected? Currently, they stay on, even after the error is fixed. I tried resetting them at the top of the script but it didn't work. Link to JSfiddle ...

How come pagination links in Vue.js 2 return JSON when clicked?

My question relates to having 2 specific components: Firstly, there is a component called SearchResultVue.vue. The structure of this component is as follows : <template> ... <span class="pull-right" v-show="totalall>8"> ...

Pseudo-element fails to display in React when applied to a paragraph tag, even with display block property set

I am experiencing an issue where the pseudo element ::after is not appearing in my browser. I am currently working with React.js and Material UI's makeStyles. Here is the code snippet causing the problem: modalTitle: { borderBottom: '2px sol ...

Encountered a failure while loading modules in AngularJS

When I tried opening the index.html page using Chrome, I encountered an error stating that the correct modules could not be found. Uncaught SyntaxError: Unexpected token < angular.js:1 Uncaught SyntaxError: Unexpected token < controller.js:1 ...