vue-chartjs 图表库配置及使用

官网

vue-chartjs 是基于 Chart.js 实现的 vue 版本

npm

npm install vue-chartjs chart.js --save

组件

Bar
HorizontalBar
Doughnut
Line
Pie
PolarArea
Radar
Bubble
Scatter
mixins
generateChert

引用

import { Bar } from 'vue-chartjs'

export default {
  extends: Bar,
  mounted () {
    this.renderChart(data, options)
  }
}

this.renderChart()方法由Bar组件提供,接收两个对象参数。第一个是你的图表数据,第二个是配置对象。在这个文档中查看你需要提供的对象结构Chart.js docs

export default {
    extends: Bar,

    props: [
        'data',
        'max',
        'min',
    ],
    mounted() {
        const {
            labels,
            data,
            bgColors,
        } = (this as any)
        .data.chartData;

        const thisTitle = (this as any).data.label;
        const units = (this as any).data.unit && (this as any).data.unit.data.find((i: any) => i);
        const tableTitle = this.$t(thisTitle) + '(' + (units) + ')';

        // const max = (this as any).data.max;
        const maxVal = Number(Math.max.apply(null, this.data.chartData.data)) * 1.2;
        const max =  Math.ceil(maxVal);
        const min = 0;

        const datacollection = {
            labels,
            datasets: [{
                backgroundColor: bgColors,
                data,
            } ],
        };

        const options: any = {
            animation: {
                duration: 100,
                onComplete: function() {
                    const chartInstance = (this as any).chart;
                    const ctx = chartInstance.ctx;
                    // ctx.font = Chart.helpers.fontString(
                    //     Chart.defaults.global.defaultFontSize,
                    //     Chart.defaults.global.defaultFontStyle,
                    //     Chart.defaults.global.defaultFontFamily
                    // );
                    ctx.textAlign = 'center';
                    // ctx.textBaseline = "bottom";
                    // ctx.fillStyle = "#F5A623";
                    (this as any).data.datasets.forEach(function(dataset, i) {
                    const meta = chartInstance.controller.getDatasetMeta(i);
                    meta.data.forEach(function(bar, index) {
                        ctx.fillText(
                        `${dataset.data[index]}`,
                        bar._model.x,
                        bar._model.y - 5,
                        );
                    });
                    });
                },
            },
            bars: {
                maxBarThickness: 20,
            },
            events: ['null'],
            responsive: true,
            maintainAspectRatio: true,
            legend: {
                display: false,
                position: 'top', // 显示的位置
                fullWidth: 'true',
     
                //   labels:{ //图例标签配置
                //         boxWidth:10 ,//彩色框的宽度
                //         fontSize:"20", //文本的字体大小
                //         fontStyle:"normal" //字体风格
                //         fontColor:"red" , // 文本的颜色
                //         padding:10 //填充行之间的彩色框
                //         fontFamily:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif" //字体家族
                //        usePointStyle:false //标签样式将匹配相应的点样式(大小基于
                //                    fontSize,在这种情况下不使用boxWidth)  
                //     
                // }        
            },
             title: {                
                display: true,
                text: tableTitle,
                position: 'top',
                // fontSize:20,   //字体大小 默认为12px
                // fontFamily:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif" //字体家族文本
                // fontColor:'#666'
                // fontStyle:'bold' //字体样式 
                // padding:10 //在标题文本上方和下方添加的像素数量
                // lineHeight:10 //单行文本的高度 
            },
            tooltips: {
                enabled: true,
                mode: 'point',
            },
            scales: {
                yAxes: [{
                    stacked: true,
                    ticks: {
                        beginAtZero: true,
                        fontSize: 11,
                        max,
                        min,
                        fontColor: '#9E9E9E',
                    },
                }],
                xAxes: [{
                    barThickness: 18,
                    ticks: {
                        fontSize: 13,
                        fontColor: '#353535',
                    },
                }],
            },
        };

        (this as any)
        .renderChart(datacollection, options);
    },
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue2项目中使用vue-chartjs,需要先安装以下依赖: ```bash npm install vue-chartjs chart.js ``` 然后在需要使用图表的组件中,引入vue-chartjs并注册组件: ```javascript import { Line } from 'vue-chartjs' export default { extends: Line, mounted () { this.renderChart({ labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Data One', backgroundColor: '#f87979', data: [40, 39, 10, 40, 39, 80, 40] } ] }, {responsive: true, maintainAspectRatio: false}) } } ``` 这里用Line为例,如果需要使用其他类型的图表,只需要替换掉对应的组件名即可。在mounted函数中,我们可以通过调用`this.renderChart`方法来渲染图表,传入的第一个参数是图表的数据,第二个参数是配置项。 当然,你也可以在组件中定义自己的数据和配置项,然后在mounted函数中调用`this.renderChart`方法来渲染图表,例如: ```javascript import { Line } from 'vue-chartjs' export default { extends: Line, data () { return { chartData: { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Data One', backgroundColor: '#f87979', data: [40, 39, 10, 40, 39, 80, 40] } ] }, chartOptions: { responsive: true, maintainAspectRatio: false } } }, mounted () { this.renderChart(this.chartData, this.chartOptions) } } ``` 这里我们将图表的数据和配置项定义在了组件的data中。 最后,在需要使用该组件的地方,直接引入即可: ```html <template> <div> <my-chart></my-chart> </div> </template> <script> import MyChart from './MyChart.vue' export default { components: { MyChart } } </script> ``` 这样就可以在Vue2项目中使用vue-chartjs了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值