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

Tips for building a responsive dropdown menu using data from a database query

Currently, I am fetching a list of companies from Firestore and attempting to display them as options in a dropdown menu. While the companies state is being populated correctly from Firestore, the options are not being created or updated dynamically. How c ...

What is the most effective approach for preventing the inadvertent override of other bound functions on window.onresize?

As I delve deeper into JavaScript, I constantly find myself pondering various aspects of it. Take for instance the window.onresize event handler. If I were to use the following code: window.onresize = resize; function resize() { console.log("resize eve ...

When attempting to send JSON data to the server using a .ajax() post request, it seems to only be

I am encountering an issue where I am attempting to insert a JSON string into the database by making a call to a web service through an AJAX request. Strangely, when I debug the code using Chrome debugger, the string gets successfully inserted into the DB. ...

Learn the steps to dynamically adjust the size of a pop-up window in Sencha based on the content that is

This is the designated container where I plan to display text. The code resides within a modal (pop-up) and it's important for the pop-up to adjust its height based on the loaded text. Ext.define('Base.view.Notes', { extend: 'Ext.Co ...

Transmit information from the frontend to the backend using JavaScript and the Express framework

After creating a custom variable in my frontend to store data, I needed to access the same data in my Express backend. The JavaScript variable and the POST request code snippet are as follows: const dataPush = { urlSave: urlSave, ...

What causes the maximum update depth exceeded error in React when trying to set data to the context?

When building my React project, I implemented a context to share the selected currency across components. While the context functionality is working well, I encountered a small issue regarding setting a default currency. At the start of the web applicati ...

Challenges with compiling Next.js with Tailwindcss and Sass

I recently created a simple template using Tailwind for my Next.js project. Normally, I rely on Tailwind's @layer components to incorporate custom CSS styles. However, this time I wanted to experiment with Sass, so I converted my globals.css file to ...

Transforming iframe programming into jquery scripting

Currently, I have implemented an Iframe loading the contents within every 5 seconds. It works well, however, there is a consistent blinking effect each time it loads which can be quite bothersome. I am looking to replace the iframe with a scrolling div so ...

Can Hibernate classes be mapped to multiple sets in Json format?

Here is a piece of code that I have mapped into Hibernate: @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="hall_id") Set<Literature> literatures; @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="hall_id") Set<RecycleFacility> rec ...

Developing secure web applications using Node.js and Express with HTTPS encryption

I am attempting to utilize express with node.js using https. Below is the relevant code for this segment: var express = require("express"); var app = express(); var https = require('https'); var privateKey = fs.readFileSync('./sslcert/myke ...

Is there a way to retrieve a specific string from a PHP response containing multiple objects?

I have a $response variable that retrieves data from a Square API call. By using var_dump($response), the following output is produced: object(Square\Http\ApiResponse)#26 (8) { ["request":"Square\Http\ApiResponse": ...

You are required to select one of the two radio buttons in order to proceed with the validation process

To prevent the user from proceeding to the next page, validation is necessary. They are required to select one of the radio buttons, etc. <div class=""> <div class="radiho" style="display: block"> <input type="checkbox" name="sp ...

Tips for creating a flexible Cell component in react-big-calendar:

Is there a way to make the cell of react-big-calendar flexible depending on the number of events, and enable scrolling? Here is a screenshot showcasing my issue: https://i.stack.imgur.com/K2h69.jpg ...

Tips on positioning a div based on the screen dimensions

In my table, there is an icon that reveals a chart as a popup when hovered over. This div is where the chart is displayed: <div id="chart-outer" style="@style" class="popup-chart close"> <h2 id="charttitle&q ...

Is there a way to ensure that the elements created by the select form are only generated once?

I have a JavaScript function that dynamically creates paragraph and input elements based on user selection in HTML. However, I am looking to optimize the code so that each element is only created once. Snippet of JavaScript code: function comFunction(sel ...

Convert a String to JSON using either JavaScript or jQuery

Currently, I am developing a JavaScript animation script and I am aiming to allow individuals to define behavior declaratively within the HTML. This is the format I envision for my HTML: <div data-animation="top: 600, left: 600, opacity: 1, start: 0.2 ...

Is it possible to select multiple drop-down lists on a webpage using Python and Selenium?

I am encountering an issue while attempting to click on multiple dropdown lists within a page. I continuously receive an error message stating that my list object does not have an attribute 'tag_name'. Here is my code snippet: def click_follow_ ...

Error encountered in Node.js: The listener must be a function

I've been working on adapting the solution provided in (How to create a simple http proxy in node.js?) from HTTP to HTTPS. However, upon attempting to access the proxy through my browser, the server abruptly stops and throws the following error: eve ...

Utilize a custom function on dynamically generated elements

I am utilizing the following jQuery function: $("#agenda_image_1,#agenda_image_2,#agenda_image_3").PictureCut({ InputOfImageDirectory : "image", PluginFolderOnServer : "../xxx/modules/xxx/assets/js/jQuery-Picture-Cut-master/", Fol ...

What happens to the npm package if I transfer ownership of a github repository to a different user?

I was considering transferring a GitHub repository to another user or organization, but I have concerns about what will happen to older versions of the npm package associated with it. Let's say my Node.js package is named node-awesome-package. Versi ...