当前位置: 首页 > news >正文

vue-chartjs使用教程

学习链接介绍

vue-chartjs文档翻译 (该作者教程介绍详细,简单易学)

chartjs官网  (data数据结构,option图标选项都可以很方便查找)

chart官网2  (常见官网)

chart所有图形例子展示 (所有图表都可看到)

vue-chartjs 是 Vue 对于 Chart.js 的封装. 你可以很简单的创建可复用的图表组件.

安装

npm install vue-chartjs chart.js --save

组件

Bar
HorizontalBar
Doughnut
Line
Pie
PolarArea
Radar
Bubble
Scatter
mixins
generateChert

引用

<script>
import { Line } from 'vue-chartjs'

export default {
  extends: Line,
  props: ['chartdata', 'options'],
  mounted () {
    this.renderChart(this.chartdata, this.options)
  }
}
</script>

<style>
</style>

 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>

 

相关文章:

  • v-echarts怎么看API
  • vue-echarts教程
  • 开发工具配置react代码(jsx)
  • react生命周期
  • TortorliseGit(小乌龟)创建删除(远程和本地)分支
  • ES6的class类的使用
  • echarts 配置项 series 中的data 多维度
  • ivew单元格合并
  • JS事件绑定三种方式
  • vue组件深层传值inheritAttrs、$attrs、$listeners
  • JS 新语法「可选链」「双问号」
  • git本地获取远程分支
  • lodash常用函数简介
  • d3函数库scale详解的使用
  • js浮点数计算加减乘除精度处理
  • [译] 怎样写一个基础的编译器
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • 2018一半小结一波
  • django开发-定时任务的使用
  • Git同步原始仓库到Fork仓库中
  • Java程序员幽默爆笑锦集
  • learning koa2.x
  • Mac转Windows的拯救指南
  • Mysql数据库的条件查询语句
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 给新手的新浪微博 SDK 集成教程【一】
  • 后端_ThinkPHP5
  • 记一次和乔布斯合作最难忘的经历
  • 力扣(LeetCode)21
  • 浅谈Golang中select的用法
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 如何选择开源的机器学习框架?
  • 深度学习入门:10门免费线上课程推荐
  • 思否第一天
  • 学习Vue.js的五个小例子
  • 一份游戏开发学习路线
  • 智能合约开发环境搭建及Hello World合约
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #Linux(Source Insight安装及工程建立)
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • (C语言)球球大作战
  • (附源码)php新闻发布平台 毕业设计 141646
  • (力扣)1314.矩阵区域和
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (强烈推荐)移动端音视频从零到上手(下)
  • (五)IO流之ByteArrayInput/OutputStream
  • (转)EOS中账户、钱包和密钥的关系
  • (转)Unity3DUnity3D在android下调试
  • (转)大型网站架构演变和知识体系
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .NET Core 2.1路线图
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Core中的去虚
  • .NET Reactor简单使用教程
  • .NET 发展历程