u-chart图表使用记录(折线图)

uni-app小程序项目

要有一个折线图数据展示,查找了很多博客,一直没法显示在小程序模拟器上,终于好了!!!记录一下

实现的效果(数据都还为0),尽力了TAT

官网http://www.mianshigee.com/tutorial/ucharts/ed9cb2b78f4d6c47.md

首先创建一个hello uni-app项目

从这个项目中可以跑起来看看图表可不可以在电脑上正常显示

按照它的路径把这个组件拷贝到自己的项目中,导入。

import uCharts from '@/components/u-charts/u-charts.js';

view层

<view class="qiun-charts">
    <canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></canvas>
</view>

CSS样式

.qiun-charts {
        width: 750upx;
        height: 500upx;
        background-color: #FFFFFF;
}
.charts {
	width: 750upx;
	height: 500upx;
	background-color: #FFFFFF;
}

script

<script>
	//引入接口方法
    import {
		myBusinessCard,
		scholar,
		statistics
	} from '../../models/user.js';
	import app from '@/App.vue';
	import uCharts from '@/components/u-charts/u-charts.js';    
    var _self;
	var canvaLineA = null;
	export default {
		data() {
			return {
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				serverData: '',
			}
		},
		onLoad() {
			_self = this;
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(500);
			this.getServerData();
		},
        methods: {
			getServerData() {
                let type = (this.show == true ? 1 : 2);
				let day = 7;
				let name = "";
				console.log(type);
				statistics(type, day).then(res => {
					if (res.Success) {
						if (res.Data != null) {
							this.serverData = data;
							console.log(res.Data);
							var shuju = res.Data;
							var d = shuju.map(a => a.Date).reverse();
							var data = shuju.map(a => a.Total).reverse(); //人数数组
							var dd = d.map(a => a.substring(a.lastIndexOf("-") + 1, a.length)); //获取日期
							var categories = dd.map(a => a.substring(a.indexOf("0") == 0 ? 1 : 0, a.length)); //去掉04日期的0,最终日期数组 
							if (type == 1) {
								this.name = "访问人数"
							} else {
								this.name = "收藏人数"
							}
							let LineA = {
								categories: [],
								series: [{
									data: [],
									name: this.name,
								}]
							}
							for (let i = 0; i < categories.length; i++) {
								let day = new Date().getDate();
								if (categories[i] == day) {
									LineA.categories.push('今日');
								} else {
									LineA.categories.push(categories[i]);
								}
								LineA.series[0].data.push(data[i]);
							}
							console.log(LineA);
							this.serverData = LineA;
							_self.showLineA("canvasLineA", LineA);
						}
						if (res.Data == null) {}
					} else {
						console.log("数据获取失败");
					}
				});
                //写的假数据,测试一下能否显示
				/* let LineA = {
					categories: ["5", "6", "7", "8", "9", "10", "11"],
					series: [{
						data: ["77", "88", "10", "0", "16", "8", "66"],
						name: "数据"
					}]
				}
				this.serverData = LineA;
				_self.showLineA("canvasLineA", LineA); */
			},
			showLineA(canvasId, chartData) {
				canvaLineA = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'line',
					fontSize: 11,
					legend: true,
					dataLabel: false,
					dataPointShape: true,
					background: '#FFFFFF',
					pixelRatio: _self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					xAxis: {
						type: 'grid',
						gridColor: '#CCCCCC',
						gridType: 'dash',
						dashLength: 8,
						disableGrid:true,    //不显示横坐标网格
					},
					yAxis: {
						gridType: 'dash',
						gridColor: '#CCCCCC',
						dashLength: 8,
						splitNumber: 5,
						min: 10,
						max: 80,
						disabled:true, //纵坐标不显示
						disableGrid:true,    //不显示纵坐标网格
					},
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					extra: {
						line: {
							type: 'straight'
						}
					}
				});
			},
            //点击显示数据
			touchLineA(e) {
				canvaLineA.showToolTip(e, {
					format: function(item, category) {
						return item.data + '人'
					}
				});
			}
		}

	}

踩坑:

后台接口获取到的数据格式要转换成所需要的格式(可以去官网看一下)

LineA.series中的name需要定义

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当然,我可以为您提供一个使用 wx-chart 在 Vue3 + Vite 中绘制折线图的示例。请按照以下步骤进行操作: 1. 创建一个新的 Vue 3 + Vite 项目: ```bash npm init vite@latest my-vue-chart-app cd my-vue-chart-app npm install ``` 2. 安装 wx-chart: ```bash npm install wx-chart ``` 3. 在 `src` 目录下创建一个新的组件 `LineChart.vue`: ```vue <template> <div> <canvas ref="chartCanvas"></canvas> </div> </template> <script> import { ref, onMounted } from 'vue'; import wxChart from 'wx-chart'; export default { name: 'LineChart', setup() { const chartCanvas = ref(null); onMounted(() => { const chart = new wxChart(chartCanvas.value, { type: 'line', data: { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [{ label: 'My Dataset', data: [65, 59, 80, 81, 56, 55, 40], fill: false, borderColor: 'rgb(75, 192, 192)', tension: 0.1 }] }, options: { scales: { y: { beginAtZero: true } } } }); }); return { chartCanvas }; } }; </script> <style> canvas { width: 100%; height: 400px; } </style> ``` 4. 在 `src` 目录下的 `App.vue` 文件中引入 `LineChart` 组件: ```vue <template> <div> <h1>Vue Chart Demo</h1> <LineChart /> </div> </template> <script> import LineChart from './LineChart.vue'; export default { name: 'App', components: { LineChart } }; </script> <style> /* 样式可以根据您的需要进行调整 */ h1 { text-align: center; } </style> ``` 5. 运行项目: ```bash npm run dev ``` 现在,您应该可以在浏览器中看到一个简单的折线图。 这只是一个基本示例,您可以根据 wx-chart 的文档进一步定制和添加其他图表类型、样式和选项。 希望对您有所帮助!如果您还有其他问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值