echarts 实现可左右滚动折线图,适用app布局

效果图:

主要代码:

<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<script src="js/mui.js"></script>
		<script src="js/jquery-3.3.1.min.js"></script>
		<script src="js/echarts.min.js"></script>
		<style>
			html,body {
				width: 100%;
				height: 100%;
				background-color: skyblue;
			}
			body {
				font-size: 12px;
				width: 100%;
				height: 100%;
				overflow: hidden;
			}
			#container {
				width: 100%;
				height: 50%;
			}
		</style>
	</head>

	<body>

		<div id="container"></div>
		<script type="text/javascript">
			mui.init();
		</script>

		<script type="text/javascript">
			var dom = document.getElementById("container");
			console.log(dom.clientWidth);
			var myChart = echarts.init(dom);

			var date = [];
			var pictorialBar = [];
			var data = [];

			for (var i = 1; i < 240; i++) {
				date.push(i + ":00");
				if (i == 21 || i == 19) {
					data.push("—");
					pictorialBar.push("—");
				} else {
					var number = Math.ceil(Math.random() * 100);
				
					pictorialBar.push({
						value: number
					})
					data.push({
						value: number,
						itemStyle: {
							color: "blue"
						},
						label: {
							color: "white"
						}
					});
				}

			}

			var max = Math.max.apply(Math, data);
			var min = Math.min.apply(Math, data);

			option = {
				animation: false,

				grid: {

					left: '0',
					right: '0',
					bottom: '30px',

				},
				tooltip: {
					trigger: 'axis',
					position: function(pt) {
						return [pt[0], '10%'];
					}
				},
				title: {
					left: 'center',
					text: null,
				},
				tooltip: {
					trigger: 'axis',
					axisPointer: { // 坐标轴指示器,坐标轴触发有效
						type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
					},
					show: false
				},
				// tooltip: {
				// 	show:false
				// },
				xAxis: {
					type: 'category',
					// boundaryGap: false,
					data: date,
					axisLabel: {
						color: "white"
					},
					axisLine: {
						lineStyle: {
							color: "white"
						}
					}
				},
				yAxis: {
					type: 'value',
					boundaryGap: [0, '100%'],
					max: 110,
					min: 0,
					show: false
				},
				dataZoom: [{
					type: 'inside',
					start: 100 - 100 / data.length * (dom.clientWidth / 40),
					end: 100,
					zoomOnMouseWheel: false,
					zoomLock: true,
				}],
				series: [{
					name: '值',
					type: 'line',
					// smooth: true,
					sampling: 'average',
					symbol: "circle",
					symbolSize: 12,
					itemStyle: {
						color: 'white'
					},
					lineStyle: {
						color: "white",
						width: 1,
						opacity: 0.8
					},
					label: {
						show: true,
						position: "top",
						// distance:10,
						formatter: "{c}",
					},
					areaStyle: {
						color: 'rgb(255, 255, 255)',
						opacity: 0.2
						// new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
						// 	offset: 0,
						// 	color: 'rgb(255, 158, 68)'
						// }, {
						// 	offset: 1,
						// 	color: 'rgb(255, 70, 131)'
						// }])
					},
					data: data
				}, {
					name: 'dotted',
					type: 'pictorialBar',
					symbol: 'rect',
					itemStyle: {
						normal: {
							color: 'rgba(255,255,255,0.5)'
						}
					},
					symbolRepeat: true,
					symbolSize: [1, 4],
					symbolMargin: 1,
					z: -10,
					data: pictorialBar
				}]
			};

			myChart.setOption(option, true);
			myChart.on('click', function(params) {
				// 控制台打印数据的名称
				console.log(params.name);
			});
		</script>
	</body>

</html>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用ECharts实现简约的折线图,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了ECharts。你可以通过在终端中运行以下命令来安装ECharts: ``` npm install echarts --save ``` 2. 在你的Vue组件中,引入ECharts并创建一个容器来显示图表。你可以使用以下代码: ```javascript <template> <div id="chartContainer" style="width: 600px; height: 400px;"></div> </template> <script> import echarts from 'echarts'; export default { mounted() { this.renderChart(); }, methods: { renderChart() { const chartContainer = document.getElementById('chartContainer'); const myChart = echarts.init(chartContainer); const option = { title: { text: '未来一周气温变化' }, tooltip: { trigger: 'axis' }, xAxis: { type: 'category', data: \['周一', '周二', '周三', '周四', '周五', '周六', '周日'\] }, yAxis: { type: 'value', axisLabel: { formatter: '{value} °C' } }, series: \[ { name: '最高气温', type: 'line', data: \[11, 11, 15, 13, 12, 13, 10\] }, { name: '最低气温', type: 'line', data: \[1, -2, 2, 5, 3, 2, 0\] } \] }; myChart.setOption(option); } } }; </script> ``` 在上面的代码中,我们首先引入了ECharts,并在`mounted`钩子函数中调用`renderChart`方法来渲染图表。在`renderChart`方法中,我们创建了一个ECharts实例,并将其绑定到指定的容器上。然后,我们定义了图表的配置项和数据,并使用`setOption`方法将其应用到图表上。 3. 最后,在你的Vue组件中使用`<chart-container>`标签来显示图表。你可以在需要显示图表的地方添加以下代码: ```html <template> <div> <chart-container></chart-container> </div> </template> <script> import ChartContainer from './ChartContainer.vue'; export default { components: { ChartContainer } }; </script> ``` 通过以上步骤,你就可以在Vue中使用ECharts实现简约的折线图了。记得根据你的需求调整图表的样式和数据。 #### 引用[.reference_title] - *1* *2* *3* [用 ECharts 做出漂亮的数据统计图](https://blog.csdn.net/qianduan666a/article/details/107182029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值