Chart JS - 如何将JSON对象输出到PIE变体中

时间:2018-03-17 13:31:19

标签: javascript chart.js

我将JSON响应拉入ChartJS饼图。阅读文档说它需要使用数据集:

public static void main(String[] args) {
    String str = "SHRtbFVuaXQgaXMgZ3JlYXQ=";
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(false);

    try{
        HtmlPage page = webClient.getPage("https://www.base64decode.org/");
        HtmlForm form = page.getFormByName("convert_text");

        HtmlTextArea content = form.getTextAreaByName("input");
        content.type(str);

        HtmlElement button = form.getButtonByName("decode");
        HtmlPage resultPage = button.click();

        HtmlTextArea contentArea = (HtmlTextArea) resultPage.getElementByName("output"); 
        String st = contentArea.getText();
        System.out.println("Result: '" + st + "'");

    } catch(Throwable e) {
        e.printStackTrace();
    }
}

对于我的用例,JSON正在拉入:

datasets: [{
    data: [10, 20, 30]
}],

在图表功能中,我正在提取[数据]。我想将{ "action": "data_link_referer", "result": { "url_ending": "0", "data": [{ "label": "Link1", "clicks": 3 }, { "label": "Link2", "clicks": 3 }, { "label": "Link3", "clicks": 2 }] } } 设置为值。

clicks

1 个答案:

答案 0 :(得分:3)

这会将您的数据转换为ChartJS的正确格式

{{1}}