【问题标题】:Django and Chart.js: Line-Chart with multiple DatasetsDjango 和 Chart.js:具有多个数据集的折线图
【发布时间】:2020-05-21 11:24:36
【问题描述】:

我正在尝试使用 chart.js 和多个数据集创建折线图(数据集的数量在其生命周期内可能会发生变化):

...来自我的“views.py”:

label = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
coworker = ['John Doe', 'Jane Doe', 'Michael Smith']
data = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [3, 4, 2, 1, 5]]

如果这是一个只有一个数据列表和一个同事的折线图,我的图表通常如下所示:

<script>
$(document).ready(function(){
    var ctx = document.getElementById('myChart1').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: {{ label|safe }},
            datasets: [{
                label: 'Some text...',
                data: {{ data|safe }},
                backgroundColor: ...,
                borderColor: ...,
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                    }
                }],
            }
        }
    });
});

我已经尝试像这样遍历我的数据...

<script>
$(document).ready(function(){
    var ctx = document.getElementById('myChart1').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: {{ label|safe }},
            datasets: [{
                label: [{% for i in label%}"{{ i }}",{% endfor %}],
                data: [{% for j in data %}{{ j }}, {% endfor %}],,
                backgroundColor: ...,
                borderColor: ...,
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                    }
                }],
            }
        }
    });
});

现在图表和从“星期一”到“星期五”的标签是可见的,但仅此而已...... 有谁知道,我做错了什么,甚至有更好的解决方案?

谢谢,祝你有美好的一天!

【问题讨论】:

  • 我也遇到了同样的情况,请问您找到动态数据集数量的解决方案了吗?

标签: javascript python django dataset chart.js


【解决方案1】:

您需要从可用数据中定义 3 个不同的数据集。

这在下面的可运行代码 sn-p 中进行了说明。不幸的是,我对python知之甚少。因此,这是一个纯 JavaScript 解决方案。然而,我认为您将能够轻松地包含您的 python 代码以获得相同的结果。

const label = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
const coworker = ['John Doe', 'Jane Doe', 'Michael Smith'];
const data = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [3, 4, 2, 1, 5]];

var myChart = new Chart('chart', {
  type: 'bar',
  data: {
    labels: label,
    datasets: [{
      label: coworker[0],
      data: data[0],
      backgroundColor: 'red'
    },
    {
      label: coworker[1],
      data: data[2],
      backgroundColor: 'blue'
    },
    {
      label: coworker[2],
      data: data[2],
      backgroundColor: 'green'
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true,
        }
      }],
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="90"></canvas>

【讨论】:

  • 感谢您的回答!我唯一的问题是,数据集的数量必须是可变的并且与 Django 兼容。不过,感谢您的解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2019-08-12
  • 2015-08-08
  • 2016-08-27
相关资源
最近更新 更多