Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

html - Javascript which can read a JSON string and make a chart of it

I'm student with no programming experience, but for a project I need to get a txt file into my Javascript chart on the HTML web page. I already tried a lot but I just cant seem it to manage it. The txt file is local and in the same folder as the html. The txt file: [[0,8],[1,10],[2,14],[3,17],[4,13],[5,8],[6,8],[7,13],[8,10],[9,13],[10,14],[11,15],[11,15]]

In this code a chart is made with values of a website, but I need to get its values from a local txt file. I have no clue how to make this work. I hope someone can help to make the code work with local txt file. code

<!DOCTYPE HTML>
<html>

<head>
  <script>
    //chart
    window.onload = function () {

      var dataPoints = [];

      var chart = new CanvasJS.Chart("chartContainer", {
        theme: "light2",
        title: {
          text: "Live Data"
        },
        data: [{
          type: "line",
          dataPoints: dataPoints
        }]
      });
      updateData();

      // Initial Values
      var xValue = 0;
      var yValue = 10;
      var newDataCount = 6;

      function addData(data) {
        if (newDataCount != 1) {
          $.each(data, function (key, value) {
            dataPoints.push({
              x: value[0],
              y: parseInt(value[1])
            });
            xValue++;
            yValue = parseInt(value[1]);
          });
        } else {
          //dataPoints.shift();
          dataPoints.push({
            x: data[0][0],
            y: parseInt(data[0][1])
          });
          xValue++;
          yValue = parseInt(data[0][1]);
        }

        newDataCount = 1;
        chart.render();
        setTimeout(updateData, 1500);
      }
      function updateData() {
        $.getJSON("https://canvasjs.com/services/data/datapoints.php?xstart=" + xValue + "&ystart=" + yValue + "&length=" + newDataCount + "type=json", addData);
      }

    }


  </script>
</head>

<body>

  <div id="chartContainer" style="height: 370px; width: 100%;"></div>
  <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
  <script src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>
</body>

</html>
question from:https://stackoverflow.com/questions/65869360/javascript-which-can-read-a-json-string-and-make-a-chart-of-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two approaches that I can think of:

Host a webserver

You can host a webserver that will expose an API that your HTML will be able to call via AJAX in order to obtain the content of your txt as a JSON. Anatomy:

  • webserver
    • is able to load your txt
    • exposes an API that can be reached via an HTTP request
    • that API will load your txt and send its content as a response
  • HTML
    • requests the webserver in order to obtain your JSON
    • uses the response as your input for the chart

Run browser from command-line

You can also create an HTML file where you will know where you need to put your JSON (a JS initialization, perhaps) and you could write a CLI application that loads your JSON from the txt, modifies your HTML file so that the JSON will be written into it and then run your browser via cli, see this example: https://www.howtogeek.com/663927/how-to-open-google-chrome-using-command-prompt-on-windows-10/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...