【问题标题】:Laravel 8 ConsoleTvs / Charts 7 Implementing Chartjs OptionsLaravel 8 ConsoleTvs / Charts 7 实现 Chartjs 选项
【发布时间】:2021-10-16 19:37:33
【问题描述】:

我目前正在更新 ConsoleTvs / 图表 6 到 7。图表正在呈现预期数据。
图表未根据图例point style 圆的任何集合options 对象键、padding 前 50 位、显示“y”和“x”轴labelString、不显示gridlines、定位@ 987654326@ 以粗体和绿色结尾,开始偏移:

        const chart = new Chartisan({
            el: '#test_chart',
            url: "@chart('test_chart_route')",
            hooks: new ChartisanHooks()
            .colors()
            .datasets(
                [
                    { type: 'line', fill: false, borderColor: '#329865', backgroundColor: '#329865' }, 
                    { type: 'line', fill: false, borderColor: '#1e5b3c', backgroundColor: '#1e5b3c' },
                    { type: 'line', fill: false, borderColor: '#0f2d1e', backgroundColor: '#0f2d1e' }, 
                    { type: 'line', fill: false, borderColor: '#329865', backgroundColor: '#329865' }, 
                    { type: 'line', fill: false, borderColor: '#1e5b3c', backgroundColor: '#1e5b3c' },
                    { type: 'line', fill: false, borderColor: '#0f2d1e', backgroundColor: '#0f2d1e' }, 
                    { type: 'line', fill: false, borderColor: '#800000', backgroundColor: '#800000' }, 
                    { type: 'line', fill: false, borderColor: '#F70000', backgroundColor: '#F70000' },
                    { type: 'line', fill: false, borderColor: '#FF6F6F', backgroundColor: '#FF6F6F' }, 
                    { type: 'line', fill: false, borderColor: '#800000', backgroundColor: '#800000' }, 
                    { type: 'line', fill: false, borderColor: '#F70000', backgroundColor: '#F70000' },
                    { type: 'line', fill: false, borderColor: '#FF6F6F', backgroundColor: '#FF6F6F' },
                ]
            ),
            options: {
                        layout: {
                            padding: {
                                left: 0,
                                right: 0,
                                top: 50,
                                bottom: 0
                            },
                        },
                        aspectRatio: 1,
                        maintainAspectRatio: false,
                        responsive: true,
                        legend: {
                            display: true,
                            position: 'top',
                            labels: {
                                usePointStyle: true,
                                fontSize: 12,
                            },
                        },
                        elements: {
                            point: {
                                pointStyle: 'circle',
                            }
                        },
                        scales: {
                            xAxes: [{
                                    maxBarThickness: 120,
                                    scaleLabel: {
                                        display: true,
                                        labelString: "xAxes_label"
                                    },
                                    gridLines: {
                                        display: false
                                    },
                                    ticks: {
                                        fontSize: 10,
                                        maxRotation: 80,
                                        minRotation: 80,
                                        padding: 2
                                    },
                            }],
                            yAxes: [{
                                    scaleLabel: {
                                        display: true,
                                        labelString: "yAxes_label"
                                    },
                                    gridLines: {
                                        display: false,
                                        drawBorder: false
                                    },
                                    ticks: {
                                        display: true,
                                        fontSize: 10,
                                        suggestedMin: 0
                                    },
                            }],
                        },
                        plugins: {
                            datalabels: {
                                color: '#ff0a6c',
                                labels: {
                                    title: {
                                        font: {
                                            weight: 'bold',
                                            size: 11,
                                        }
                                    },
                                    value: {
                                        color: 'green'
                                    }
                                },
                                formatter: function(value, context) {
                                    return (value != '' && value !== undefined) ? Math.round(value * 100) / 100 : value;
                                },
                                anchor: 'end',
                                align: 'start',
                                display: 'auto',
                                clamp: false
                            }
                        }
                    }
        });

我也尝试使用 ChartisanHooks().options({...options...}) 方法,但设置选项没有变化。

谁能告诉我正确的方向?

【问题讨论】:

    标签: charts package chart.js laravel-8 php-7.4


    【解决方案1】:

    您没有使用options 挂钩,而是将options 定义为属性。你应该使用.options({ ... }),而不是options: { ... }

    options 钩子对我不起作用,也许我使用了一个过时的 Chartisan 库。根据Custom Hooks,也可以将options合并到图表中,如下:

    .custom(function({ data, merge }) {
       return merge(data, {
         options: {
           ...
         }
       }),
    })
    

    请在下面查看您修改后的可运行代码,看看它如何与merge 函数一起使用。

    const chart = new Chartisan({
      el: '#test_chart',
      data: {
        "chart": {
          "labels": ["First", "Second", "Third"]
        },
        "datasets": [{
            "name": "Sample 1",
            "values": [10, 3, 7]
          },
          {
            "name": "Sample 2",
            "values": [1, 6, 2]
          }
        ]
      },
      hooks: new ChartisanHooks()
        .colors()
        .datasets(
          [{
              type: 'line',
              fill: false,
              borderColor: '#329865',
              backgroundColor: '#329865'
            },
            {
              type: 'line',
              fill: false,
              borderColor: '#1e5b3c',
              backgroundColor: '#1e5b3c'
            }
          ]
        )
        .custom(function({ data, merge }) {
          return merge(data, {
            options: {
              layout: {
                padding: {
                  left: 0,
                  right: 0,
                  top: 50,
                  bottom: 0
                },
              },
              aspectRatio: 1,
              maintainAspectRatio: false,
              responsive: true,
              legend: {
                display: true,
                position: 'top',
                labels: {
                  usePointStyle: true,
                  fontSize: 12,
                },
              },
              elements: {
                point: {
                  pointStyle: 'circle',
                }
              },
              scales: {
                xAxes: [{
                  maxBarThickness: 120,
                  scaleLabel: {
                    display: true,
                    labelString: "xAxes_label"
                  },
                  gridLines: {
                    display: false
                  },
                  ticks: {
                    fontSize: 10,
                    maxRotation: 80,
                    minRotation: 80,
                    padding: 2
                  },
                }],
                yAxes: [{
                  scaleLabel: {
                    display: true,
                    labelString: "yAxes_label"
                  },
                  gridLines: {
                    display: false,
                    drawBorder: false
                  },
                  ticks: {
                    display: true,
                    fontSize: 10,
                    suggestedMin: 0
                  },
                }],
              },
              plugins: {
                datalabels: {
                  color: '#ff0a6c',
                  labels: {
                    title: {
                      font: {
                        weight: 'bold',
                        size: 11,
                      }
                    },
                    value: {
                      color: 'green'
                    }
                  },
                  formatter: function(value, context) {
                    return (value != '' && value !== undefined) ? Math.round(value * 100) / 100 : value;
                  },
                  anchor: 'end',
                  align: 'start',
                  display: 'auto',
                  clamp: false
                }
              }
            }
          });
        }),
    });
    <script src="https://unpkg.com/chart.js@2.9.4/dist/Chart.min.js"></script>
    <script src="https://unpkg.com/@chartisan/chartjs@^2.1.0/dist/chartisan_chartjs.umd.js"></script>
    <div id="test_chart" style="height: 300px;"></div>

    【讨论】:

    • 谢谢。有用。文档确实说用户应该使用optionsChartisanHooks 方法而不是custom 方法。这种自定义方法使包比它的前身好一点。超级容易使用,更新是一个爆炸,它只是一个快速开发的陡峭学习曲线,据我所知,不允许在路线上进行本地化。你可能知道为什么后端 advancedDataset 方法没有在前端使用 extra 参数数据集配置数组吗?
    • @Hmerman6006:很高兴它对你有用!很遗憾,我无法回答您关于 advancedDataset 的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2016-07-25
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多