热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据可视化(1)--Chart.js

来源:https:www.cnblogs.comCraryPrimitiveManp3469271.htmlChart.js是一个HTML5图表库,使用canvas元素来展示各式各

来源:https://www.cnblogs.com/CraryPrimitiveMan/p/3469271.html

Chart.js是一个HTML5图表库,使用canvas元素来展示各式各样的客户端图表,支持折线图、柱形图、雷达图、饼图、环形图等。在每种图表中,还包含了大量的自定义选项,包括动画展示形式。 Chart.js比较轻量(gzip版本仅4.5k),且不依赖其他库。

项目官网:http://www.chartjs.org/

曲线图(Line chart)
doctype html>
<html>
<head>
<title>Line Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas
{
}
style>
head>
<body>
<canvas id="canvas" height="450" width="600">canvas>


<script>

var lineChartData = {
       
// x轴的标示
labels : ["January","February","March","April","May","June","July"],
       
// 数据,数组中的每一个object代表一条线
datasets : [
{
            
// 颜色的使用类似于CSS,你也可以使用RGB、HEX或者HSL
            // rgba颜色中最后一个值代表透明度
            // 填充颜色
fillColor : "rgba(220,220,220,0.5)",
            
// 线的颜色
strokeColor : "rgba(220,220,220,1)",
            
// 点的填充颜色
pointColor : "rgba(220,220,220,1)",
            
// 点的边线颜色
pointStrokeColor : "#fff",
            
// 与x轴标示对应的数据
data : [65,59,90,81,56,55,40]
},
{
fillColor :
"rgba(151,187,205,0)",
strokeColor :
"rgba(151,187,205,1)",
pointColor :
"rgba(151,187,205,1)",
pointStrokeColor :
"#fff",
data : [
28,48,40,19,96,27,100]
}
]

}

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

script>
body>
html>

效果如下

图表选项

    var optiOns= {
//Boolean - If we show the scale above the chart data
// 网格线是否在数据线的上面
scaleOverlay : false,

//Boolean - If we want to override with a hard coded scale
// 是否用硬编码重写y轴网格线
scaleOverride : false,

//** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
// y轴刻度的个数
scaleSteps : null,
//Number - The value jump in the hard coded scale
// y轴每个刻度的宽度
scaleStepWidth : null,
//Number - The scale starting value
// y轴的起始值
scaleStartValue : null,

//String - Colour of the scale line
// x轴y轴的颜色
scaleLineColor : "rgba(0,0,0,1)",

//Number - Pixel width of the scale line
// x轴y轴的线宽
scaleLineWidth : 1,

//Boolean - Whether to show labels on the scale
// 是否显示y轴的标签
scaleShowLabels : true,

//Interpolated JS string - can access value
// 标签显示值
scaleLabel : "<%=value%>",

//String - Scale label font declaration for the scale label
// 标签的字体
scaleFontFamily : "'Arial'",

//Number - Scale label font size in pixels
// 标签字体的大小
scaleFontSize : 12,

//String - Scale label font weight style
// 标签字体的样式
scaleFontStyle : "normal",

//String - Scale label font colour
// 标签字体的颜色
scaleFontColor : "#666",

///Boolean - Whether grid lines are shown across the chart
// 是否显示网格线
scaleShowGridLines : true,

//String - Colour of the grid lines
// 网格线的颜色
scaleGridLineColor : "rgba(0,0,0,.1)",

//Number - Width of the grid lines
// 网格线的线宽
scaleGridLineWidth : 1,

//Boolean - Whether the line is curved between points
// 是否是曲线
bezierCurve : true,

//Boolean - Whether to show a dot for each point
// 是否显示点
pointDot : true,

//Number - Radius of each point dot in pixels
// 点的半径
pointDotRadius : 3,

//Number - Pixel width of point dot stroke
// 点的线宽
pointDotStrokeWidth : 1,

//Boolean - Whether to show a stroke for datasets
datasetStroke : true,

//Number - Pixel width of dataset stroke
// 数据线的线宽
datasetStrokeWidth : 3,

//Boolean - Whether to fill the dataset with a colour
// 数据线是否填充颜色
datasetFill : true,

//Boolean - Whether to animate the chart
// 是否有动画效果
animation : true,

//Number - Number of animation steps
// 动画的步数
animationSteps : 60,

//String - Animation easing effect
// 动画的效果
animationEasing : "easeOutQuart",

//Function - Fires when the animation is complete
// 动画完成后调用
onAnimationComplete : null
}

添加选项后,调用如下

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData, options);

柱状图(Bar chart)
doctype html>
<html>
<head>
<title>Bar Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas
{
}
style>
head>
<body>
<canvas id="canvas" height="450" width="600">canvas>


<script>

var barChartData = {
labels : [
"January","February","March","April","May","June","July"],
datasets : [
{
fillColor :
"rgba(220,220,220,0.5)",
strokeColor :
"rgba(220,220,220,1)",
data : [
65,59,90,81,56,55,40]
},
{
fillColor :
"rgba(151,187,205,0.5)",
strokeColor :
"rgba(151,187,205,1)",
data : [
28,48,40,19,96,27,100]
}
]

}

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);

script>
body>
html>

效果如下

图表选项,内容基本如上,不一一注释

Bar.defaults = {

//Boolean - If we show the scale above the chart data
scaleOverlay : false,

//Boolean - If we want to override with a hard coded scale
scaleOverride : false,

//** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The scale starting value
scaleStartValue : null,

//String - Colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)",

//Number - Pixel width of the scale line
scaleLineWidth : 1,

//Boolean - Whether to show labels on the scale
scaleShowLabels : false,

//Interpolated JS string - can access value
scaleLabel : "<%=value%>",

//String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'",

//Number - Scale label font size in pixels
scaleFontSize : 12,

//String - Scale label font weight style
scaleFontStyle : "normal",

//String - Scale label font colour
scaleFontColor : "#666",

///Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,

//String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)",

//Number - Width of the grid lines
scaleGridLineWidth : 1,

//Boolean - If there is a stroke on each bar
barShowStroke : true,

//Number - Pixel width of the bar stroke
barStrokeWidth : 2,

//Number - Spacing between each of the X value sets
// 柱状块与x值所形成的线(如:x=20这条线)之间的距离
barValueSpacing : 5,

//Number - Spacing between data sets within X values
// 在同一x值内的柱状块之间的间距
barDatasetSpacing : 1,

//Boolean - Whether to animate the chart
animation : true,

//Number - Number of animation steps
animationSteps : 60,

//String - Animation easing effect
animationEasing : "easeOutQuart",

//Function - Fires when the animation is complete
onAnimationComplete : null

}

雷达图或蛛网图(Radar chart)
doctype html>
<html>
<head>
<title>Radar Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas
{
}
style>
head>
<body>
<canvas id="canvas" height="450" width="450">canvas>


<script>

var radarChartData = {
labels : [
"Eating","Drinking","Sleeping","Designing","Coding","Partying","Running"],
datasets : [
{
fillColor :
"rgba(220,220,220,0.5)",
strokeColor :
"rgba(220,220,220,1)",
pointColor :
"rgba(220,220,220,1)",
pointStrokeColor :
"#fff",
data : [
65,59,90,81,56,55,40]
},
{
fillColor :
"rgba(151,187,205,0.5)",
strokeColor :
"rgba(151,187,205,1)",
pointColor :
"rgba(151,187,205,1)",
pointStrokeColor :
"#fff",
data : [
28,48,40,19,96,27,100]
}
]

}

var myRadar = new Chart(document.getElementById("canvas").getContext("2d")).Radar(radarChartData,{scaleShowLabels : false, pointLabelFontSize : 10});

script>
body>
html>

效果如下

图表选项,内容基本如上,不一一注释

Radar.defaults = {

//Boolean - If we show the scale above the chart data
scaleOverlay : false,

//Boolean - If we want to override with a hard coded scale
scaleOverride : false,

//** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The centre starting value
scaleStartValue : null,

//Boolean - Whether to show lines for each scale point
scaleShowLine : true,

//String - Colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)",

//Number - Pixel width of the scale line
scaleLineWidth : 1,

//Boolean - Whether to show labels on the scale
scaleShowLabels : false,

//Interpolated JS string - can access value
scaleLabel : "<%=value%>",

//String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'",

//Number - Scale label font size in pixels
scaleFontSize : 12,

//String - Scale label font weight style
scaleFontStyle : "normal",

//String - Scale label font colour
scaleFontColor : "#666",

//Boolean - Show a backdrop to the scale label
scaleShowLabelBackdrop : true,

//String - The colour of the label backdrop
scaleBackdropColor : "rgba(255,255,255,0.75)",

//Number - The backdrop padding above & below the label in pixels
scaleBackdropPaddingY : 2,

//Number - The backdrop padding to the side of the label in pixels
scaleBackdropPaddingX : 2,

//Boolean - Whether we show the angle lines out of the radar
// 是否显示角度线
angleShowLineOut : true,

//String - Colour of the angle line
// 角度线的颜色
angleLineColor : "rgba(0,0,0,.1)",

//Number - Pixel width of the angle line
// 角度线的线宽
angleLineWidth : 1,

//String - Point label font declaration
pointLabelFontFamily : "'Arial'",

//String - Point label font weight
pointLabelFontStyle : "normal",

//Number - Point label font size in pixels
pointLabelFontSize : 12,

//String - Point label font colour
pointLabelFontColor : "#666",

//Boolean - Whether to show a dot for each point
pointDot : true,

//Number - Radius of each point dot in pixels
pointDotRadius : 3,

//Number - Pixel width of point dot stroke
pointDotStrokeWidth : 1,

//Boolean - Whether to show a stroke for datasets
datasetStroke : true,

//Number - Pixel width of dataset stroke
datasetStrokeWidth : 2,

//Boolean - Whether to fill the dataset with a colour
datasetFill : true,

//Boolean - Whether to animate the chart
animation : true,

//Number - Number of animation steps
animationSteps : 60,

//String - Animation easing effect
animationEasing : "easeOutQuart",

//Function - Fires when the animation is complete
onAnimationComplete : null

}

极地区域图(Polar area chart)
doctype html>
<html>
<head>
<title>Polar Area Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
style>
head>
<body>
<canvas id="canvas" height="400" width="400">canvas>


<script>
var chartData = [
{
value : Math.random(),
color:
"#D97041"
},
{
value : Math.random(),
color:
"#C7604C"
},
{
value : Math.random(),
color:
"#21323D"
},
{
value : Math.random(),
color:
"#9D9B7F"
},
{
value : Math.random(),
color:
"#7D4F6D"
},
{
value : Math.random(),
color:
"#584A5E"
}
];
var myPolarArea = new Chart(document.getElementById("canvas").getContext("2d")).PolarArea(chartData);
script>
body>
html>

效果如下

图表选项

PolarArea.defaults = {

//Boolean - Whether we show the scale above or below the chart segments
scaleOverlay : true,

//Boolean - If we want to override with a hard coded scale
scaleOverride : false,

//** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The centre starting value
scaleStartValue : null,

//Boolean - Show line for each value in the scale
scaleShowLine : true,

//String - The colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)",

//Number - The width of the line - in pixels
scaleLineWidth : 1,

//Boolean - whether we should show text labels
scaleShowLabels : true,

//Interpolated JS string - can access value
scaleLabel : "<%=value%>",

//String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'",

//Number - Scale label font size in pixels
scaleFontSize : 12,

//String - Scale label font weight style
scaleFontStyle : "normal",

//String - Scale label font colour
scaleFontColor : "#666",

//Boolean - Show a backdrop to the scale label
scaleShowLabelBackdrop : true,

//String - The colour of the label backdrop
scaleBackdropColor : "rgba(255,255,255,0.75)",

//Number - The backdrop padding above & below the label in pixels
scaleBackdropPaddingY : 2,

//Number - The backdrop padding to the side of the label in pixels
scaleBackdropPaddingX : 2,

//Boolean - Stroke a line around each segment in the chart
segmentShowStroke : true,

//String - The colour of the stroke on each segement.
segmentStrokeColor : "#fff",

//Number - The width of the stroke value in pixels
segmentStrokeWidth : 2,

//Boolean - Whether to animate the chart or not
animation : true,

//Number - Amount of animation steps
animationSteps : 100,

//String - Animation easing effect.
animationEasing : "easeOutBounce",

//Boolean - Whether to animate the rotation of the chart
animateRotate : true,

//Boolean - Whether to animate scaling the chart from the centre
animateScale : false,

//Function - This will fire when the animation of the chart is complete.
onAnimationComplete : null
}

饼图(Pie chart)
doctype html>
<html>
<head>
<title>Radar Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas
{
}
style>
head>
<body>
<canvas id="canvas" height="450" width="450">canvas>


<script>

var pieData = [
{
value:
30,
color:
"#F38630"
},
{
value :
50,
color :
"#E0E4CC"
},
{
value :
100,
color :
"#69D2E7"
}

];

var myPie = new Chart(document.getElementById("canvas").getContext("2d")).Pie(pieData);

script>
body>
html>

效果如下

图表选项

Pie.defaults = {
//Boolean - Whether we should show a stroke on each segment
// 块和块之间是否有间距
segmentShowStroke : true,

//String - The colour of each segment stroke
  // 块和块之间间距的颜色
segmentStrokeColor : "#fff",

//Number - The width of each segment stroke
  // 块和块之间间距的宽度
segmentStrokeWidth : 2,

//Boolean - Whether we should animate the chart
animation : true,

//Number - Amount of animation steps
animationSteps : 100,

//String - Animation easing effect
animationEasing : "easeOutBounce",

//Boolean - Whether we animate the rotation of the Pie
  // 是否有从0度到360度的动画
animateRotate : true,

//Boolean - Whether we animate scaling the Pie from the centre
  // 是否有从中心到边缘的动画
animateScale : false,

//Function - Will fire on animation completion.
onAnimationComplete : null
}

环形图(Doughnut chart)
doctype html>
<html>
<head>
<title>Doughnut Charttitle>
<script src="../Chart.js">script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas
{
}
style>
head>
<body>
<canvas id="canvas" height="450" width="450">canvas>


<script>

var doughnutData = [
{
value:
30,
color:
"#F7464A"
},
{
value :
50,
color :
"#46BFBD"
},
{
value :
100,
color :
"#FDB45C"
},
{
value :
40,
color :
"#949FB1"
},
{
value :
120,
color :
"#4D5360"
}

];

var myDoughnut = new Chart(document.getElementById("canvas").getContext("2d")).Doughnut(doughnutData);

script>
body>
html>

效果如下

图标选项

Doughnut.defaults = {
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke : true,

//String - The colour of each segment stroke
segmentStrokeColor : "#fff",

//Number - The width of each segment stroke
segmentStrokeWidth : 2,

//The percentage of the chart that we cut out of the middle.
percentageInnerCutout : 50,

//Boolean - Whether we should animate the chart
animation : true,

//Number - Amount of animation steps
animationSteps : 100,

//String - Animation easing effect
animationEasing : "easeOutBounce",

//Boolean - Whether we animate the rotation of the Doughnut
animateRotate : true,

//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false,

//Function - Will fire on animation completion.
onAnimationComplete : null
}

最后发现,这篇博客基本就是从官网的文档上摘下来的,不过加上了中文注释,聊胜于无吧


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Oracle10g备份导入的方法及注意事项
    本文介绍了使用Oracle10g进行备份导入的方法及相关注意事项,同时还介绍了2019年独角兽企业重金招聘Python工程师的标准。内容包括导出exp命令、删用户、创建数据库、授权等操作,以及导入imp命令的使用。详细介绍了导入时的参数设置,如full、ignore、buffer、commit、feedback等。转载来源于https://my.oschina.net/u/1767754/blog/377593。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
author-avatar
可爱嘟嘟豬5
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有