【问题标题】:Display No Record found message inside Bar Chart using Chart.js使用 Chart.js 在条形图中显示未找到记录消息
【发布时间】:2019-08-13 10:43:56
【问题描述】:

我想在条形图中没有数据时显示文本消息。我正在使用react-chart-js-2 和以下代码来显示消息,但它不起作用。我不知道出了什么问题,但我认为这可能有效,因为我指的是这个小提琴https://jsfiddle.net/x04ptfuu/

Chart.plugins.register({
  afterDraw: function(chart) {
    if (chart.data.datasets.length === 0) {
      // No data is present
      var ctx = chart.chart.ctx;
      var width = chart.chart.width;
      var height = chart.chart.height
      chart.clear();    
      ctx.save();
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.font = "16px normal 'Helvetica Nueue'";
      ctx.fillText('No data to display', width / 2, height / 2);
      ctx.restore();
    }
  }
});

我的条形图画布元素可用于 react chartjs 2:

<Bar data={this.state.chart2Data}
    height={650}
    width={1200}
    options={{
        title: {
        display: true,
        text: 'Repetitive Observations - Review 2',
        fontSize: 16,
        fontColor: '#000000'
        },
        legend: {
            display: true,
            position: 'top',
            labels: {
                fontColor: '#000000'
            }

        },
        maintainAspectRatio: false,
        plugins: {
            datalabels: {
               display: function(context) {
                return context.dataset.data[context.dataIndex] !== 0},
               color: '#ffffff'
            }
         },
        responsive: false,
        scales: {
            xAxes:[{
              scaleLabel: {
                  display: true,
                  labelString: 'Activity',
                  fontStyle: 'bold',
                  fontColor: '#000000',
                  fontSize: 13
                },
                ticks: {
                    fontColor: '#000000',
                    minRotation: 90
                }
            }],
            yAxes:[{
                ticks: {
                    fontColor: '#000000'
                    }
            }]
        }
    }}  
/>

谁能帮忙?

【问题讨论】:

    标签: javascript reactjs charts bar-chart


    【解决方案1】:

    您所指的fiddle 纯粹是Javascript

    在 React 中你有 Conditional Rendering

    {this.state.chart2Data && this.state.chart2Data.length > 0 ? <Bar data = {this.state.chart2Data}
        height = {
            650
        }
        width = {
            1200
        }
        options = {
            {
                title:
                {
                    display: true,
                    text: 'Repetitive Observations - Review 2',
                    fontSize: 16,
                    fontColor: '#000000'
                },
                legend:
                {
                    display: true,
                    position: 'top',
                    labels:
                    {
                        fontColor: '#000000'
                    }
    
                },
                maintainAspectRatio: false,
                plugins:
                {
                    datalabels:
                    {
                        display: function(context)
                        {
                            return context.dataset.data[context.dataIndex] !== 0
                        },
                        color: '#ffffff'
                    }
                },
                responsive: false,
                scales:
                {
                    xAxes: [
                    {
                        scaleLabel:
                        {
                            display: true,
                            labelString: 'Activity',
                            fontStyle: 'bold',
                            fontColor: '#000000',
                            fontSize: 13
                        },
                        ticks:
                        {
                            fontColor: '#000000',
                            minRotation: 90
                        }
                    }],
                    yAxes: [
                    {
                        ticks:
                        {
                            fontColor: '#000000'
                        }
                    }]
                }
            }
        }
        />
        :
        <div> No Data to display </div>
    }
    

    【讨论】:

    • 即使它有效,但我想清除图表并在图表本身上显示消息。使用条件方法,我将无法正确格式化并在空图表中显示消息
    • 在这种情况下,您需要使用一些 CSS 在图表上显示文本。
    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多