简体   繁体   English

使用JSON将分类数据从SQL导入到C3.js

[英]Importing Categorical data from SQL, to C3.js, using JSON

I'm currently working on dynamically building column graphs in MVC, using C3 Charts. 我目前正在使用C3图表在MVC中动态构建列图。 While I can bring the data from the database using AJAX/JSON, I am able to bind only the Y-Axis coordinates using the JSON property of C3, not the X-Axis categories. 虽然我可以使用AJAX / JSON从数据库中获取数据,但我能够使用C3的JSON属性仅绑定Y轴坐标,而不是X轴类别。

What I want to do: Build column charts dynamically. 我想做什么:动态构建柱形图。 For instance, the marks of students of multiple years will be calibrated as (Year 1: 50),(Year 2: 100) and so on, with the first part (category:Year) going on the X-Axis and the numeric part (Marks) going to the Y-Axis. 例如,多年学生的分数将被校准为(1年级:50年),(2年级:100年级),依此类推,第一部分(类别:年份)将在X轴和数字部分进行(马克斯)去了Y轴。 The number of such combinations is dynamic, depending on the user's requirements (brought from the database as two columns, with multiple rows). 这种组合的数量是动态的,具体取决于用户的要求(从数据库中作为两列,带有多行)。

Problems I'm running into: 我遇到的问题:

  1. Specifying two different data types in the keys renders the non-numeric part (the category) as a separate series altogether, which I don't want and which isn't even working. 在键中指定两种不同的数据类型会将非数字部分(类别)作为一个单独的系列呈现,这是我不想要的,哪些甚至不起作用。

  2. Whatever I do, the X-Axis comes with whole numbers depending on the number of columns visible. 无论我做什么,X轴都带有整数,具体取决于可见的列数。 I'd like to change that to the actual category names. 我想将其更改为实际的类别名称。

Here is what I am doing. 这就是我在做的事情。 I took inspiration from Daniel's method here ( https://groups.google.com/d/msg/c3js/RV1X18GZoGY/-p39m9Ngt-gJ ) 我从Daniel的方法中获取灵感( https://groups.google.com/d/msg/c3js/RV1X18GZoGY/-p39m9Ngt-gJ

    $.ajax({
    url: <Method in Controller here>,
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(parameters),
    success: function (data) {
            var myData = jQuery.parseJSON(data);
            var testJson = $.map(myData, function (item) {
                return {
                    Year: item.Year,
                    Marks: item.Marks

                }
            });

            var chart = c3.generate({
                bindto: '#chart',
                size: {
                    width: 800
                },
                data: {
                    json: testJson,

                    mimeType: 'json',
                    keys: {
                        value: ['Marks']
                    },

                }
            });
    }
});

Looking forward to some assistance. 期待一些帮助。 Thanks in advance! 提前致谢!

Edit: I want the data to look like this in the end: Sample Chart 编辑:我希望数据最终看起来像这样: 示例图表

You can try the following code: 您可以尝试以下代码:

var jsonTest =  [
                   { year: 1, marks: 50 },
                   { year: 2, marks: 70 },
                   { year: 3, marks: 75 },
                   { year: 4, marks: 45 },
                ];

// The variable name chart will automatically bind itself to an id = chart 
var chart = c3.generate({
    data: {                
        type : 'bar',
        json: jsonTest,
        keys: {
            x: 'year',
            value: ['marks']
        }
    },
    size: {
        width: 400,
    },
    axis: {
        x: {
            type: 'category'
        }
    },
    bar: {
        width: {
            ratio: 0.5
        }
    }
});

I was getting the following output: 我得到以下输出:

条形图

Here's the JSBin example: Bar Chart Example 这是JSBin示例: 条形图示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM