首页 > 其他分享 >HTML5 canvas画一个折线图

HTML5 canvas画一个折线图

时间:2023-02-14 12:01:58浏览次数:36  
标签:canvas lineTo space height item let HTML5 context 折线图


HTML5 canvas画一个折线图_html5

<body>
<canvas id="drawing" style="border:1px solid;" width="600" height="400"></canvas>
<script type="text/javascript">
let drawing = document.querySelector("#drawing");
let context = drawing.getContext("2d");
//1,画线
context.strokeStyle = '#ccc';
let space = 10; //每条线的间距
let width = context.canvas.width; //600 画布的宽度
let height = context.canvas.height; //400 画布的高度
let arrow = 10; //坐标轴箭头的中线
let arrowFloor = 10; //坐标轴箭头的底线
//竖线
for (var i = 0; i < width / space; i++) {
context.beginPath();
context.moveTo(i * space - 0.5, 0);
context.lineTo(i * space - 0.5, height)
context.stroke();
}
//横线
for (var i = 0; i < height / space; i++) {
context.beginPath();
context.moveTo(0, i * space - 0.5);
context.lineTo(width, i * space - 0.5)
context.stroke();
}
//画坐标系
//圆心坐标
let x0 = space-0.5;
let y0 = height - space-0.5;
//y 轴
context.strokeStyle = '#333';
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(x0, space);
context.stroke();
//y轴的箭头
context.beginPath();
context.moveTo(x0, space);
context.lineTo(x0 - arrowFloor / 2, space + arrow);
context.lineTo(x0 + arrowFloor / 2, space + arrow);
context.lineTo(x0, space);
context.fill();
context.stroke();
//x 轴
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(width - space, height - space);
//x 轴箭头
context.lineTo(width - space - arrow, height - space - arrowFloor / 2);
context.lineTo(width - space - arrow, height - space + arrowFloor / 2);
context.lineTo(width - space, height - space);
context.fill();
context.stroke();

//封装一个方法-画点-方形square
function drawSquare(element, x, y, w, h) {
element.beginPath();
element.moveTo(x - w / 2, y - h / 2);
element.lineTo(x + w / 2, y - h / 2);
element.lineTo(x + w / 2, y + h / 2);
element.lineTo(x - w / 2, y + h / 2);
element.closePath();
element.fill();
element.stroke();
}
//数据
let data = [{
x: 100,
y: 100
}, {
x: 150,
y: 220
},{
x: 250,
y: 120
},{
x: 350,
y: 220
},{
x: 390,
y: 300
},{
x: 440,
y: 320
}];
//将数据由转换成canvas上的数据

//连线
let prevX = space+data[0].x;//上一个点的坐标 为连线做准备
let prevY = height-space-data[0].y;
data.forEach((item,index,data)=>{
item.x = space+item.x;
item.y = height-space-item.y;
//画点
drawSquare(context, item.x, item.y, 5, 5);
//连线
context.beginPath();
context.moveTo(prevX,prevY);
context.lineTo(item.x,item.y);
context.stroke();
prevX = item.x;
prevY = item.y;
})




</script>
</body>


标签:canvas,lineTo,space,height,item,let,HTML5,context,折线图
From: https://blog.51cto.com/u_15964288/6056536

相关文章

  • HTML5 canvas基础使用
    <body> <!-- 1,canvas的宽和高要在标签中写,写在style中将会使画布拉伸到指定宽和高,不是真正的宽高。 --> <canvasid='drawing'width="300"height="700"......
  • 折线图
    <!--折线图--><template><divclass="chartBox"><!--echart图标--><divid="lineChart"ref="lineChart"></div></div></template><script>//引入基本模板//......
  • python的折线图实现的具体案例
    #导入包frompyecharts.chartsimportLinefrompyecharts.optionsimportTitleOpts,ToolboxOpts,LegendOpts,VisualMapOpts,LabelOptsimportjson#处理数据f_us=open("D......
  • 用canvas怎么制作下雪效果
    首先新建一个html文件,将body的背景设置为天空的那种深蓝色,并创建一个canvas,canvas的操作逻辑都放在snow.js中:<!DOCTYPEhtml><head><style>body{backgro......
  • canvas模拟鼠标拉框;画折线;(基于VUE)
    <template><divref="mouseDiv"style="background-color:transparent;width:100%;height:100%;position:absolute;top:0;left:0;z-index:2023;"><div......
  • canvas + Cesium 动画图片材质
    letosm=Cesium.createOpenStreetMapImageryProvider({url:"https://a.tile.openstreetmap.org/",})letviewer=newCesium.Viewer(thi......
  • vue+echarts实现疫情折线图
    效果:代码:<<template><div><divid="left1"style="height:800px;width:100%"></div></div></template><script>//疫情数据//exportdefault{data()......
  • 大学生学html5课堂笔记
    html 如果时间允许,会涉及到html5Css 如果时间允许,会涉及到css3学习的技术,主要为html4+css2如果你有HBuilder sublim vsCode,可不选择下载HBuilderX前端三剑客:HTML+CS......
  • vue实现canvas画很多条线,点击那条线就删除那条线需求。
    公司最近提了个新需求,就是要在canvas画板上要画许多条线,点击那条线就删除那条线的功能,刚开始我用的原生canvas,很麻烦并不容易实现。于是改变思路,用到了'KonvaJS'1、KonvaJ......
  • 基于 WebGL 的 HTML5 楼宇自控 3D 可视化监控
    前言智慧楼宇和人们的生活息息相关,楼宇智能化程度的提高,会极大程度的改善人们的生活品质,在当前工业互联网大背景下受到很大关注。目前智慧楼宇可视化监控的主要优点包括:智......