繁体   English   中英

在 C# 使用 QuickChart 创建图表

[英]Create a chart with QuickChart in C#

我想创建一个图表,但我无法从列表发送数据,我正在使用这个库,因为它允许我创建 web 个链接。 有人可以帮我吗,我正在使用 c#。我想更改列表的月份

Chart qc = new Chart();
            qc.Width = 500;
            qc.Height = 300;
            qc.Version = "2.9.4";
            qc.Config = @"{
                          type: 'bar',
                          data: {
                            labels: ['January', 'February', 'March', 'April', 'May'],
                            datasets: [
                              { label: 'Dogs', data: [50, 60, 70, 180, 190] },
                            ],
                          },
                          options: {
                          scales: {
                            xAxes: [
                              {
                                scaleLabel: {
                                  display: true,
                                  fontColor: '#00ff00',
                                  fontSize: 20,
                                  fontStyle: 'bold',
                                  labelString: 'Month',
                                },
                              },
                            ],
                            yAxes: [
                              {
                                scaleLabel: {
                                  display: true,
                                  labelString: '# Users',
                                  fontColor: '#ff0000',
                                  fontSize: 20,
                                  fontStyle: 'bold',
                                },
                              },
                            ]
                          }
                        }
                        }";

您可以使用字符串插值将数据动态添加到字符串中,然后使用String.Join方法将它们格式化以匹配 QuickChart 格式,注意必须使用双花括号来转义必要的大括号

        List<string> labels = new List<string>() { "'Jan'", "'Feb'", "'Jul'", "'Nov'" };
        List<int> data = new List<int>() { 100, 300, 400, 500 };
        Chart qc = new Chart();

        qc.Width = 500;
        qc.Height = 300;
        qc.Version = "2.9.4";
        qc.Config = $@"{{
            type: 'bar',
            data: {{
                labels: [ {string.Join(",", labels)} ],
                datasets: [{{
                    label: 'Users',
                    data: [ {string.Join(",", data)} ]
                }}]
            }}
        }}";

        Console.WriteLine(qc.GetUrl());

您可能需要创建自己的方法来将字符串数据包装到所需的引号中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM