• <bdo id='O88HD'></bdo><ul id='O88HD'></ul>

      <small id='O88HD'></small><noframes id='O88HD'>

      <tfoot id='O88HD'></tfoot>

      <legend id='O88HD'><style id='O88HD'><dir id='O88HD'><q id='O88HD'></q></dir></style></legend>

      1. <i id='O88HD'><tr id='O88HD'><dt id='O88HD'><q id='O88HD'><span id='O88HD'><b id='O88HD'><form id='O88HD'><ins id='O88HD'></ins><ul id='O88HD'></ul><sub id='O88HD'></sub></form><legend id='O88HD'></legend><bdo id='O88HD'><pre id='O88HD'><center id='O88HD'></center></pre></bdo></b><th id='O88HD'></th></span></q></dt></tr></i><div id='O88HD'><tfoot id='O88HD'></tfoot><dl id='O88HD'><fieldset id='O88HD'></fieldset></dl></div>

        chartjs 时间笛卡尔轴适配器和日期库设置

        时间:2023-11-02
        1. <i id='wfJ6n'><tr id='wfJ6n'><dt id='wfJ6n'><q id='wfJ6n'><span id='wfJ6n'><b id='wfJ6n'><form id='wfJ6n'><ins id='wfJ6n'></ins><ul id='wfJ6n'></ul><sub id='wfJ6n'></sub></form><legend id='wfJ6n'></legend><bdo id='wfJ6n'><pre id='wfJ6n'><center id='wfJ6n'></center></pre></bdo></b><th id='wfJ6n'></th></span></q></dt></tr></i><div id='wfJ6n'><tfoot id='wfJ6n'></tfoot><dl id='wfJ6n'><fieldset id='wfJ6n'></fieldset></dl></div>
            <tbody id='wfJ6n'></tbody>
        2. <legend id='wfJ6n'><style id='wfJ6n'><dir id='wfJ6n'><q id='wfJ6n'></q></dir></style></legend>
            • <bdo id='wfJ6n'></bdo><ul id='wfJ6n'></ul>

              <small id='wfJ6n'></small><noframes id='wfJ6n'>

                  <tfoot id='wfJ6n'></tfoot>
                  本文介绍了chartjs 时间笛卡尔轴适配器和日期库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试实施本教程,但无法完成.https://www.chartjs.org/docs/latest/axes/笛卡尔/time.html

                  I am trying to implement this tutorial and could not get it done. https://www.chartjs.org/docs/latest/axes/cartesian/time.html

                  输入:具有(时间,值)属性的对象列表.Time 是 Integer,表示 unix 时间以秒为单位;值为浮点数.

                  Input: list of objects with (time,value) attributes. Time is Integer that means unix time in seconds; value is Float.

                  教程说日期适配器.时间刻度需要同时存在日期库相应的适配器.请从可用的适配器中选择".

                  The tutorial says "Date Adapters. The time scale requires both a date library and a corresponding adapter to be present. Please choose from the available adapters".

                  日期库,date-fns:https://github.com/date-fns/date-fns

                  问题 1. 如何安装/包含 date-fns 库?文档说npm",但我认为它仅适用于 Node.js,但我有一个 Django 项目,Ubuntu.如果我只是下载 zip,里面有一堆文件.

                  Question 1. how to install/include the date-fns library? The documentation says "npm", but I think it is only for Node.js, but I have a Django project, Ubuntu. If I just download the zip, there is a bunch of files inside.

                  适配器,chartjs-adapter-date-fns https://github.com/chartjs/chartjs-adapter-date-fns.

                  问题2.如何安装fns适配器?文档说npm",但我有一个 Django 项目,Ubuntu.但是,如果我包含 <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>,我觉得够了.

                  Question 2. how to install the fns adapter? The documentation says "npm", but I have a Django project, Ubuntu. BUT, if I include <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>, I feel it is enough.

                  问题3.安装适配器和日期库后,如何修复下面的脚本以使情节正常(时间笛卡尔轴)?我认为这都是关于更新行 point["x"] = elem.time; 以将 unix 时间转换为某种适当的类型.

                  Question 3. after installing adapter and date library, how to fix the script below to make the plot work (Time Cartesian Axis)? I think it is all about updating line point["x"] = elem.time; to convert a unix time to some appropriate type.

                  HTML

                  <canvas id="myChart"></canvas>
                  

                  JS

                     let points = [];
                      for(let elem of objs) {
                          point = {};
                          point["x"] = elem.time;
                          point["y"] = elem.value;
                          points.push(point);
                      }
                  
                      var ctx = document.getElementById('myChart').getContext('2d');
                      var chart = new Chart(ctx, {
                          // The type of chart we want to create
                          type: 'line',
                  
                          // The data for our dataset
                          data: points,
                  
                          // Configuration options go here
                          options: {
                              responsive: false,
                  
                              scales: {
                              x: {
                                  type: 'time',
                              }
                          }
                          }
                      });
                  

                  推荐答案

                  确实可以使用脚本标签来安装所有 3 个必需的库,请参阅下面的示例.

                  Installing all the 3 required libs can indeed be done using script tags, see live example underneath.

                  您的数据未显示的原因是因为 chart.js 不希望数据字段中有数据数组.在数据字段中,它需要一个对象,该对象至少具有所有 datasets 的键,该键是一个数组和一个可选的标签数组,但是由于您对数据使用对象格式,因此不需要标签数组.

                  The reason your data doesnt show is because chart.js doesnt expect a data array in the data field. In the data field it expects an object with at least a key for all the datasets which is an array and an optional labels array, but since you are using object format for your data the labels array is not neccesarry.

                  每个数据集都有自己的图例标签,在数据集对象中,您可以在数据字段中配置数据数组.查看实时示例:

                  Each dataset has its own label for the legend and in the dataset object you configure the data array in the data field. See live example:

                  const options = {
                    type: 'line',
                    data: {
                      datasets: [{
                        label: '# of Votes',
                        data: [{
                          x: 1632664468243,
                          y: 5
                        }, {
                          x: 1632664458143,
                          y: 10
                        }],
                        borderColor: 'pink'
                      }]
                    },
                    options: {
                      scales: {
                        x: {
                          type: 'time'
                        }
                      }
                    }
                  }
                  
                  const ctx = document.getElementById('tt').getContext('2d');
                  new Chart(ctx, options);

                  <canvas id="tt"></canvas>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>
                  <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>

                  这篇关于chartjs 时间笛卡尔轴适配器和日期库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Chart.js 获取图表上当前可见点的数组 下一篇:Chartjs 隐藏数据集图例 v3

                  相关文章

                  1. <i id='qclE6'><tr id='qclE6'><dt id='qclE6'><q id='qclE6'><span id='qclE6'><b id='qclE6'><form id='qclE6'><ins id='qclE6'></ins><ul id='qclE6'></ul><sub id='qclE6'></sub></form><legend id='qclE6'></legend><bdo id='qclE6'><pre id='qclE6'><center id='qclE6'></center></pre></bdo></b><th id='qclE6'></th></span></q></dt></tr></i><div id='qclE6'><tfoot id='qclE6'></tfoot><dl id='qclE6'><fieldset id='qclE6'></fieldset></dl></div>
                  2. <small id='qclE6'></small><noframes id='qclE6'>

                    • <bdo id='qclE6'></bdo><ul id='qclE6'></ul>
                  3. <tfoot id='qclE6'></tfoot>
                    <legend id='qclE6'><style id='qclE6'><dir id='qclE6'><q id='qclE6'></q></dir></style></legend>