Learn how to combine pie and bar charts using Highcharts. Discover how to efficiently load JSON data and understand the different ways

I'm feeling a bit lost when it comes to loading json data into the Highcharts combo pie/bar chart. Below is an example code that's a work in progress. I just need some help understanding how to load the json and structure the data series correctly. Appreciate any assistance!

  1. Is the highlighted code section correct? The project is on my local development server and the json file is also local.

  2. Starting with the Status and ID values from the json file, how should the data series be formatted in the JavaScript code?

For instance:

name: 'id',
data: processed_json

-Begin Code example html/js-

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>xxxxxxxx Automation</title>

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <style type="text/css">
${demo.css}
        </style>
        <script type="text/javascript">
$(function () {

    //  Load json data
    // var processed_json = new Array();
    //$.getJSON('json_output.json', function(data) {
    //    // Populate series
    //    for (i = 0; i < data.length; i++){
    //        processed_json.push([data[i].key, data[i].value]);


    $('#container').highcharts({
        title: {
            text: 'xxxxxxxxxxxxxx'
        },
        xAxis: {
            categories: ['Nightly Scheduled Smoke Test', 'New Release Test', 'Regression Test', ' Discovery Test']
        },
        labels: {
            items: [{
                html: 'xxxxxxxxx Validation',
                style: {
                    left: '55px',
                    top: '18px',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                }
            }]
        },
        series: [{
            type: 'column',
            name: 'Ad-Hoc',
            data: [3, 5, 5, 8]
        }, {
            type: 'column',
            name: 'Create Order',
            data: [3, 5, 5, 8]
        }, {
            type: 'column',
            name: 'Create-Express-Ready-Parameter',
            data: [3, 5, 5, 8]

        }, {
            type: 'spline',
            name: 'Average',
            data: [3, 2.67, 3, 6.33, 3.33],
            marker: {
                lineWidth: 2,
                lineColor: Highcharts.getOptions().colors[2],
                fillColor: 'white'
            }
        }, {
            type: 'pie',
            name: 'xxxxxxx Passed',
            data: [{
                name: 'Ad-Hoc',
                y: 13,
                color: Highcharts.getOptions().colors[0] // Ad-Hoc's color
            }, {
                name: 'Create Order',
                y: 23,
                color: Highcharts.getOptions().colors[1] // Create Order's color
            }, {
                name: 'Create-Express-Ready-Parameter',
                y: 19,
                color: Highcharts.getOptions().colors[2] // Create-Express-Ready-Parameter's color
            }],
            center: [100, 80],
            size: 100,
            showInLegend: false,
            dataLabels: {
                enabled: false
            }
        }]
    });
});
    </script>
    </head>
    <body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
<p><center>
The following XXXXXn Report illustrates current Pass validation by test category.
<p> * Green represents the number of passed scenarios
<p> * Black represents the number of scenarios that are in progress or dev/test.
<p> * Blue represents the number of failed scenarios.
</p>
</center>
</html>

-snippet of json-

[
  {
    "uri": "features/TC-0001-Bill-Of-Laiding-Shipping.feature",
    "id": "check-pricing-for-bill-of-laiding",
    "keyword": "Feature",
    "name": "Check Pricing for Bill of Laiding",
    "description": "",
    "line": 3,
    "tags": [
      {
        "name": "@Bill-Of-Laiding-Shipping",
        "line": 1
      }
    ],
    "elements": [
      {
        "id": "check-pricing-for-bill-of-laiding;check-pricing-for-bill-of-laiding",
        "keyword": "Scenario",
        "name": "Check Pricing for Bill of Laiding",
        "description": "",
        "line": 5,
        "type": "scenario",
        "tags": [
          {
            "name": "@Bill-Of-Laiding-Shipping",
            "line": 1
          }
        ],
        "steps": [
          {
            "keyword": "Given ",
            "name": "TC-0006-Log into One Lisa",
            "line": 7,
            "match": {
              "location": "features/step_definitions/my_steps.rb:779"
            },
            "result": {
              "status": "passed",
              "duration": 9024120000
            }
          },
          {
            "keyword": "Then ",
            "name": "TC-0001-Bill-Of-Laiding Query onleLisa (Lisa_One) to access existing Bill Of laiding",
            "line": 8,
            "match": {
              "location": "features/step_definitions/my_steps.rb:1194"
            },

Answer №1

Allow me to simplify your code for better understanding. Your JSON data should be inserted into the 'dataPie' variable that I have created.

I trust this will be beneficial to you.

$(document).ready(function() {
    var dataPie = [
        {
            name: 'FIRST PART OF PIE',
            y: 10,
            color: Highcharts.getOptions().colors[0]
        }, {
            name: 'SECOND PART OF PIE',
            y: 15,
            color: Highcharts.getOptions().colors[1]
        }];

    var pieObject = {
            type: 'pie',
            name: 'xxxxxxx Passed',
            data: dataPie,
            center: [100, 80],
            size: 100,
            showInLegend: false,
            dataLabels: {
                enabled: false
            }
        }



    $('#container').highcharts({
        title: {
            text: 'xxxxxxxxxxxxxx'
        },
        xAxis: {
            categories: ['Nightly Scheduled Smoke Test', 'New Release Test', 'Regression Test', ' Discovery Test']
        },
        labels: {
            items: [{
                html: 'xxxxxxxxx Validation',
                style: {
                    left: '55px',
                    top: '18px',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                }
            }]
        },
        series: [{
            type: 'column',
            name: 'Ad-Hoc',
            data: [3, 5, 5, 8]
        }, {
            type: 'column',
            name: 'Create Order',
            data: [3, 5, 5, 8]
        }, {
            type: 'column',
            name: 'Create-Express-Ready-Parameter',
            data: [3, 5, 5, 8]

        }, {
            type: 'spline',
            name: 'Average',
            data: [3, 2.67, 3, 6.33, 3.33],
            marker: {
                lineWidth: 2,
                lineColor: Highcharts.getOptions().colors[2],
                fillColor: 'white'
            }
        }, 
            pieObject
        ]
    });
})

Answer №2

1) The Highcharts Documentation provides valuable information on custom data preprocessing, specifically when loading data from JSON using jQuery's $.getJSON. It is important to include the data in the chart's configuration object before creating the chart.

$(document).ready(function() {
    var options = {
        chart: {
            renderTo: 'container',
            type: 'spline'
        },
        series: [{}]
    };
    $.getJSON('data.json', function(data) {
        options.series[0].data = data;
        var chart = new Highcharts.Chart(options);
    });
});

2) Refer to the Series documentation for details on how it should be structured. The data array is a crucial component for plotting the chart and must be formatted according to Highcharts specifications - more guidance can be found in the API documentation.

To summarize - the data must be in one of three formats: an array of numerical values(1), an array of arrays(2), or an array of objects with named properties(3). Each point's data must include the 'y' values and may also contain additional information.

I am unable to assist with specific data parsing without knowing the required values or the specifics of the data you wish to include in the chart. If you provide more details, I can update this response with a more tailored example or demonstration.

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

What is the process for creating a transparent default color theme in MUI?

I'm looking to make an element's background color the primary main color with some transparency using Material-UI. I've attempted a couple of methods, but unfortunately neither have worked for me. Any suggestions or guidance would be greatly ...

React: image appearing in DOM but failing to render on screen

As I work on developing a web application using React, my focus is on creating cards that consist of header text and an image. However, I have encountered a problem with displaying the image properly. When I import the image and attempt to use it in my co ...

What could be causing my ajax file uploader to malfunction?

I am currently working on building an AJAX file upload module. Below is a snippet of the code I have been using: //creating a form for uploading files via AJAX FileUploader.prototype.createForm = function() { // creating a new form var form = docu ...

Listening to changes in a URL using JQuery

Is there a way to detect when the browser URL has been modified? I am facing the following situation: On my webpage, I have an iframe that changes its content and updates the browser's URL through JavaScript when a user interacts with it. However, no ...

Magento issue with unresponsive image map functionality

My website is built using the Ultimo theme in Magento, and I have implemented a script from to make the image map responsive. Here is the HTML code: <img name="sale" src="sale_1.jpg" width="1180" height="200" id="sale" usemap="#m_sale" alt="" />< ...

Using jQuery to dynamically insert variables into CSS styles

I am attempting to use jQuery 3 to modify the CSS of a class and change the background color. My first step is converting an rgba color code (properties.color) to a hexadecimal code, which is functioning correctly. Next, I try to change the background co ...

Tips for retrieving data from a JSON response with the specified structure

When I make a request, the response is as follows: { "data": [ "http:\/\/www.domain.com.br\/anunciantes\/jorgediaz.y.com.r\/26\/img1.jpg", "http:\/\/www.domain.com.br\/anunciantes\/jorg ...

Creating a HMAC-SHA-256 signature in JavaScript for datatrans: A step-by-step guide

I'm currently working on an Angular project that involves implementing the Datatrans payment system. Unfortunately, I have been facing difficulties in generating a sign for the payment. I have been following the process outlined in this link (enter l ...

Struggling with JavaScript conditional statements

Currently, I am in the process of creating a login and registration panel using jQuery and PHP. Essentially, if there are any errors during registration, it sets certain form errors, redirects back to the registration page, the JavaScript identifies these ...

Utilize SoundCloud API to generate user profiles according to their unique identification within the system

In my system, I have implemented a process that takes an array of SoundCloud user IDs. These IDs are then iterated through a series of SC.get functions to gather information about each user, such as their user ID, username, followings, and genre preference ...

The server's file URLs are modified within the page source of a WordPress site

I've been attempting to integrate Adsense code into a WordPress blog at demonuts.com. I placed the Google code in the TEXT WIDGET provided by WordPress. However, upon running the website, I noticed that the URLs for .js, .css, or .png files are being ...

The unexpected blank space appearing beneath my website as a result of images and videos placed on the

There seems to be some random white space on my website between the main body elements and the footer. Interestingly, removing the cat image and videoplayer eliminates this white space. However, I don't want to remove them completely, so I'm tryi ...

Experiencing difficulty in choosing an array in Javascript

Currently learning Javascript and working on a project that involves retrieving data from a weather API. Encountered a simple issue that I need help with. Here is the data I managed to fetch so far: {coord: {…}, weather: Array(1), base: "stations", mai ...

Test success despite Cypress assertion failing

Conducting integration tests on an API. Encountering a scenario where one test passes while another fails despite having similar assertions. Feeling confused about the handling of async/promises in cypress. context("Login", () => { // This t ...

Working with multiple onClick events in jQuery

I have multiple onClick events that toggle hide and display classes on various elements when clicked. While it is functional, the amount of code involved makes it appear cluttered and unwieldy. $('.info_2').on('click', function() { $ ...

Custom HTML binding in expanding rows of Angular 2 DataTables

I am currently working on implementing a data table feature that allows for an extended child row to be displayed when clicking the + icon. This row will show additional data along with some buttons that are bound via AJAX before transitioning to Angular 2 ...

Encountering a problem with Vue when running the node-rdkafka package

I added node-rdkafka to my project using the command npm install node-rdkafka. Then, I included it in my code like this: import Kafka from 'node-rdkafka'; created() { console.log(Kafka.features); } An error occurred when running npm r ...

Using middleware in Express to handle GET requests

Can the request object sent to any route be accessed globally in Express, without having to explicitly access it in a .get method or similar? ...

Vue should only activate the element that has been clicked on

I'm currently working on my first Vue project and encountering an issue with triggering a child component within a table cell. Whenever I double click a cell, the `updated` event is being triggered in all child components associated with the table cel ...

Determine the total hours and minutes elapsed between two specific dates and times

Looking for some assistance here. I have a form where users need to input a start time and end time of an incident. After entering this information, they would manually calculate the duration between the two date times. I am attempting to streamline this p ...