0

I am Unity beginner, so sorry in advance if my problem is easy to fix, I spent five days on it and still don't have the solution. I am trying to display chart generated by highcharts.com like this one: Clink to see the chart. I am working with Unity and Vuforia. This is my code so far:

json1.json (I took it from the highcharts website)

{

        "chart": { "type": "line" },
        "title": { "text": "My Chart" },
        "subtitle": { "text": "My Untitled Chart" },
        "exporting": {},
        "series": [
            {
                "turboThreshold": 0,
                "_symbolIndex": 0,
                "name": "Level of decibels",
                "marker": {},
                "_colorIndex": 0
            }
        ],
        "plotOptions": {
            "series": {
                "dataLabels": {},
                "animation": false
            }
        },
        "tooltip": {},
        "legend": {},
        "lang": {},
        "credits": {},
        "yAxis": { "title": {} },
        "data": {
            "csvURL": "here is my url to csv",
            "enablePolling": true,
            "dataRefreshRate": "2"
        },
        "googleSpreadsheetKey": false,
        "googleSpreadsheetWorksheet": false
    }

converterJson.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

public class ConverterJson : MonoBehaviour {

    string path;
    string jsonString;

    void Start()
    {
        path = Application.streamingAssetsPath + "/json1.json";
        jsonString = File.ReadAllText(path);

        RootObject myObject = JsonUtility.FromJson<RootObject>(jsonString);

    }

}

[Serializable]
public class Chart
{
    public string type { get; set; }
}

[Serializable]
public class Title
{
    public string text { get; set; }
}

[Serializable]
public class Subtitle
{
    public string text { get; set; }
}

[Serializable]
public class Exporting
{
}

[Serializable]
public class Marker
{
}

[Serializable]
public class Series
{
    public int turboThreshold { get; set; }
    public int _symbolIndex { get; set; }
    public string name { get; set; }
    public Marker marker { get; set; }
    public int _colorIndex { get; set; }
}

[Serializable]
public class DataLabels
{
}

[Serializable]
public class Series2
{
    public DataLabels dataLabels { get; set; }
    public bool animation { get; set; }
}

[Serializable]
public class PlotOptions
{
    public Series2 series { get; set; }
}

[Serializable]
public class Tooltip
{
}

[Serializable]
public class Legend
{
}

[Serializable]
public class Lang
{
}

[Serializable]
public class Credits
{
}

[Serializable]
public class Title2
{
}

[Serializable]
public class YAxis
{
    public Title2 title { get; set; }
}

[Serializable]
public class Data
{
    public string csvURL { get; set; }
    public bool enablePolling { get; set; }
    public string dataRefreshRate { get; set; }
}

[Serializable]
public class RootObject
{
    public Chart chart { get; set; }
    public Title title { get; set; }
    public Subtitle subtitle { get; set; }
    public Exporting exporting { get; set; }
    public List<Series> series { get; set; }
    public PlotOptions plotOptions { get; set; }
    public Tooltip tooltip { get; set; }
    public Legend legend { get; set; }
    public Lang lang { get; set; }
    public Credits credits { get; set; }
    public YAxis yAxis { get; set; }
    public Data data { get; set; }
    public bool googleSpreadsheetKey { get; set; }
    public bool googleSpreadsheetWorksheet { get; set; }
}

To get Serializable classes I converted json1.json file using json2scharp website. To display the chart I dragged ConverterJson script to the plane 3D object. Everytime when I run the program, the plane is blank. I don't have any errors, just one warning:

warning CS0219: The variable `myObject' is assigned but its value is never used

I know that I missed something in my code, but I am not sure how to fix it. Thank you for your answers in advance!

  • Read the troubleshooting section of the answer from the duplicate. One of the things you have to do is remove `{ get; set; }` from your objects because `JsonUtility` cannot serialize/de-serialize those (properties). – Programmer Jul 20 '18 at 15:46
  • Hi, you marked my question as duplicate, so where can I find the link to see the other question? – Caroline1512 Jul 20 '18 at 15:51
  • [Here](https://i.imgur.com/ZH1nVHY.png) you go! You seem to have ignored my `{ get; set; }` comment because that should fix your issue even without looking at the duplicate. – Programmer Jul 20 '18 at 15:54
  • 1
    I just thought there is something more to do. Thank you, I changed my code as you said – Caroline1512 Jul 20 '18 at 15:56
  • My issue is not fixed I am afraid – Caroline1512 Jul 20 '18 at 16:04
  • What's the issue? `myObject ` is null? Did you try to access it's value? – Programmer Jul 20 '18 at 16:10
  • First thing, I am not sure if I should write RootObject here: RootObject myObject = JsonUtility.FromJson(jsonString); ? Second thing - accessing? Do you mean create the plane variable inside the code and make it equal to that line above? – Caroline1512 Jul 20 '18 at 16:13
  • I though were having issues reading the json file? That was the issue, right? – Programmer Jul 20 '18 at 16:14
  • Yes, reading the json file is my issue. As I said, I am a beginner and I saw w few tutorials to be able to write what I have so far. But after your suggestion I still have warning and I don't see the chart as an output. – Caroline1512 Jul 20 '18 at 16:16
  • You haven't even tried to use the deserialized result. o something like `Debug.Log(myObject.data.csvURL)` and see if you get *"here is my url to csv"* as out put – Programmer Jul 20 '18 at 16:25
  • I see the url as an output, but I want to see the proper chart (not in the console but via camera). – Caroline1512 Jul 20 '18 at 16:40
  • Oh I see. You'll need to make one. You can use Unity's LineRenderer to draw lines. If you can't do that then buy a "Graph And Chart" plugin from the assetstore to do it. If you really want to do it yourself there are many tutorials for Unity LineRenderer on the internet. When you try it and and run into issues, create a new question with your new code. – Programmer Jul 20 '18 at 16:45

0 Answers0