Commit a56998c5 authored by 吴颖's avatar 吴颖

'修改图表样式'

parent 89e69734
import WxCanvas from './wx-canvas';
import * as echarts from './echarts.min.js';
let ctx;
function compareVersion(v1, v2) {
v1 = v1.split('.')
v2 = v2.split('.')
const len = Math.max(v1.length, v2.length)
while (v1.length < len) {
v1.push('0')
}
while (v2.length < len) {
v2.push('0')
}
for (let i = 0; i < len; i++) {
const num1 = parseInt(v1[i])
const num2 = parseInt(v2[i])
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
}
}
return 0
}
Component({
properties: {
canvasId: {
type: String,
value: 'ec-canvas'
},
ec: {
type: Object
},
forceUseOldCanvas: {
type: Boolean,
value: false
}
},
data: {
isUseNewCanvas: false
},
ready: function () {
// Disable prograssive because drawImage doesn't support DOM as parameter
// See https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html
echarts.registerPreprocessor(option => {
if (option && option.series) {
if (option.series.length > 0) {
option.series.forEach(series => {
series.progressive = 0;
});
}
else if (typeof option.series === 'object') {
option.series.progressive = 0;
}
}
});
if (!this.data.ec) {
console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
+ 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
return;
}
if (!this.data.ec.lazyLoad) {
this.init();
}
},
methods: {
init: function (callback) {
const version = wx.getSystemInfoSync().SDKVersion
const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
const forceUseOldCanvas = this.data.forceUseOldCanvas;
const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
this.setData({ isUseNewCanvas });
if (forceUseOldCanvas && canUseNewCanvas) {
console.warn('开发者强制使用旧canvas,建议关闭');
}
if (isUseNewCanvas) {
// console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
// 2.9.0 可以使用 <canvas type="2d"></canvas>
this.initByNewWay(callback);
} else {
const isValid = compareVersion(version, '1.9.91') >= 0
if (!isValid) {
console.error('微信基础库版本过低,需大于等于 1.9.91。'
+ '参见:https://github.com/ecomfe/echarts-for-weixin'
+ '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
return;
} else {
console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
this.initByOldWay(callback);
}
}
},
initByOldWay(callback) {
// 1.9.91 <= version < 2.9.0:原来的方式初始化
ctx = wx.createCanvasContext(this.data.canvasId, this);
const canvas = new WxCanvas(ctx, this.data.canvasId, false);
echarts.setCanvasCreator(() => {
return canvas;
});
// const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
const canvasDpr = 1
var query = wx.createSelectorQuery().in(this);
query.select('.ec-canvas').boundingClientRect(res => {
if (typeof callback === 'function') {
this.chart = callback(canvas, res.width, res.height, canvasDpr);
}
else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
}
else {
this.triggerEvent('init', {
canvas: canvas,
width: res.width,
height: res.height,
canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
});
}
}).exec();
},
initByNewWay(callback) {
// version >= 2.9.0:使用新的方式初始化
const query = wx.createSelectorQuery().in(this)
query
.select('.ec-canvas')
.fields({ node: true, size: true })
.exec(res => {
const canvasNode = res[0].node
this.canvasNode = canvasNode
const canvasDpr = wx.getSystemInfoSync().pixelRatio
const canvasWidth = res[0].width
const canvasHeight = res[0].height
const ctx = canvasNode.getContext('2d')
const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
echarts.setCanvasCreator(() => {
return canvas
})
if (typeof callback === 'function') {
this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
} else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
} else {
this.triggerEvent('init', {
canvas: canvas,
width: canvasWidth,
height: canvasHeight,
dpr: canvasDpr
})
}
})
},
canvasToTempFilePath(opt) {
if (this.data.isUseNewCanvas) {
// 新版
const query = wx.createSelectorQuery().in(this)
query
.select('.ec-canvas')
.fields({ node: true, size: true })
.exec(res => {
const canvasNode = res[0].node
opt.canvas = canvasNode
wx.canvasToTempFilePath(opt)
})
} else {
// 旧的
if (!opt.canvasId) {
opt.canvasId = this.data.canvasId;
}
ctx.draw(true, () => {
wx.canvasToTempFilePath(opt, this);
});
}
},
touchStart(e) {
if (this.chart && e.touches.length > 0) {
var touch = e.touches[0];
var handler = this.chart.getZr().handler;
handler.dispatch('mousedown', {
zrX: touch.x,
zrY: touch.y
});
handler.dispatch('mousemove', {
zrX: touch.x,
zrY: touch.y
});
handler.processGesture(wrapTouch(e), 'start');
}
},
touchMove(e) {
if (this.chart && e.touches.length > 0) {
var touch = e.touches[0];
var handler = this.chart.getZr().handler;
handler.dispatch('mousemove', {
zrX: touch.x,
zrY: touch.y
});
handler.processGesture(wrapTouch(e), 'change');
}
},
touchEnd(e) {
if (this.chart) {
const touch = e.changedTouches ? e.changedTouches[0] : {};
var handler = this.chart.getZr().handler;
handler.dispatch('mouseup', {
zrX: touch.x,
zrY: touch.y
});
handler.dispatch('click', {
zrX: touch.x,
zrY: touch.y
});
handler.processGesture(wrapTouch(e), 'end');
}
}
}
});
function wrapTouch(event) {
for (let i = 0; i < event.touches.length; ++i) {
const touch = event.touches[i];
touch.offsetX = touch.x;
touch.offsetY = touch.y;
}
return event;
}
\ No newline at end of file
{
"component": true,
"usingComponents": {}
}
\ No newline at end of file
<!-- 新的:接口对其了H5 -->
<canvas wx:if="{{isUseNewCanvas}}" type="2d" class="ec-canvas" canvas-id="{{ canvasId }}" bindinit="init" bindtouchstart="{{ ec.disableTouch ? '' : 'touchStart' }}" bindtouchmove="{{ ec.disableTouch ? '' : 'touchMove' }}" bindtouchend="{{ ec.disableTouch ? '' : 'touchEnd' }}"></canvas>
<!-- 旧的 -->
<canvas wx:else class="ec-canvas" canvas-id="{{ canvasId }}" bindinit="init" bindtouchstart="{{ ec.disableTouch ? '' : 'touchStart' }}" bindtouchmove="{{ ec.disableTouch ? '' : 'touchMove' }}" bindtouchend="{{ ec.disableTouch ? '' : 'touchEnd' }}"></canvas>
\ No newline at end of file
.ec-canvas {
width: 100%;
height: 100%;
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
export default class WxCanvas {
constructor(ctx, canvasId, isNew, canvasNode) {
this.ctx = ctx;
this.canvasId = canvasId;
this.chart = null;
this.isNew = isNew
if (isNew) {
this.canvasNode = canvasNode;
}
else {
this._initStyle(ctx);
}
// this._initCanvas(zrender, ctx);
this._initEvent();
}
getContext(contextType) {
if (contextType === '2d') {
return this.ctx;
}
}
// canvasToTempFilePath(opt) {
// if (!opt.canvasId) {
// opt.canvasId = this.canvasId;
// }
// return wx.canvasToTempFilePath(opt, this);
// }
setChart(chart) {
this.chart = chart;
}
attachEvent() {
// noop
}
detachEvent() {
// noop
}
_initCanvas(zrender, ctx) {
zrender.util.getContext = function () {
return ctx;
};
zrender.util.$override('measureText', function (text, font) {
ctx.font = font || '12px sans-serif';
return ctx.measureText(text);
});
}
_initStyle(ctx) {
var styles = ['fillStyle', 'strokeStyle', 'globalAlpha',
'textAlign', 'textBaseAlign', 'shadow', 'lineWidth',
'lineCap', 'lineJoin', 'lineDash', 'miterLimit', 'fontSize'];
styles.forEach(style => {
Object.defineProperty(ctx, style, {
set: value => {
if (style !== 'fillStyle' && style !== 'strokeStyle'
|| value !== 'none' && value !== null
) {
ctx['set' + style.charAt(0).toUpperCase() + style.slice(1)](value);
}
}
});
});
ctx.createRadialGradient = () => {
return ctx.createCircularGradient(arguments);
};
}
_initEvent() {
this.event = {};
const eventNames = [{
wxName: 'touchStart',
ecName: 'mousedown'
}, {
wxName: 'touchMove',
ecName: 'mousemove'
}, {
wxName: 'touchEnd',
ecName: 'mouseup'
}, {
wxName: 'touchEnd',
ecName: 'click'
}];
eventNames.forEach(name => {
this.event[name.wxName] = e => {
const touch = e.touches[0];
this.chart.getZr().handler.dispatch(name.ecName, {
zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
zrY: name.wxName === 'tap' ? touch.clientY : touch.y
});
};
});
}
set width(w) {
if (this.canvasNode) this.canvasNode.width = w
}
set height(h) {
if (this.canvasNode) this.canvasNode.height = h
}
get width() {
if (this.canvasNode)
return this.canvasNode.width
return 0
}
get height() {
if (this.canvasNode)
return this.canvasNode.height
return 0
}
}
\ No newline at end of file
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"@vant/weapp": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@vant/weapp/-/weapp-1.0.7.tgz",
"integrity": "sha512-F7gkcVKLTh4Pt17ukZnpZmn78CbtfmUtxSZif02ysyXZmqtAf1aAUffL3i1y+qzGWCXF4y/OVkOBVQVrKHI4MA=="
},
"vant-weapp": {
"version": "0.5.27",
"resolved": "https://registry.npmjs.org/vant-weapp/-/vant-weapp-0.5.27.tgz",
"integrity": "sha512-wmbpPWbpIk/Nl022Yfu0LNV+rSBPxnWTsZePVo6De4FxY3SWw47+Ip6rxLgdnG00/FntbymgvVw4xlaW+WmC+Q=="
}
}
}
const app = getApp() const app = getApp()
let Charts = require('../../../utils/wxcharts-min.js'); import {
var lineChart = null; Base
import {Base} from '../../../utils/base.js'; } from '../../../utils/base.js';
const base = new Base() const base = new Base()
import * as echarts from '../../../ec-canvas/echarts.min.js';
let chartLine;
Page({ Page({
data: { data: {
windowWidth: '', windowWidth: '',
windowHeight: '', windowHeight: '',
coach : '', coach: '',
scoreList: [], scoreList: [],
coachcomment:'', coachcomment: '',
awardList: [], awardList: [],
arr1: [], class_id: '',
arr2:[], lesson_id: '',
class_id:'', child_id: '',
lesson_id :'', class_single_id: '',
child_id :'', ec: {
class_single_id : '' onInit: function (canvas, width, height) {
chartLine = echarts.init(canvas, null, {
width: width,
height: height
});
canvas.setChart(chartLine);
}
}
}, },
onLoad: function (options) { onLoad: function (options) {
console.log(options)
this.setData({ this.setData({
class_id : options.class_id, coach_id: app.globalData.coach_id,
lesson_id : options.lesson_id, class_id: options.class_id,
child_id : options.child_id, lesson_id: options.lesson_id,
class_single_id : options.class_single_id child_id: options.child_id,
class_single_id: options.class_single_id
}) })
console.log(options.class_single_id)
this.getRpx()
this.getcomment() this.getcomment()
this.getbadges() this.getbadges()
}, },
getRpx() { getcomment: function () {
var res = wx.getSystemInfoSync(); //试图获取屏幕宽高数据
this.setData({
windowWidth: res.windowWidth / 750 * 690 ,
windowHeight: res.windowWidth / 750 * 605
})
},
getCharts() {
lineChart = new Charts({
canvasId: 'radarCanvas',
type: 'radar',
categories: this.data.arr1,
legend: false,
series: [{
data: this.data.arr2,
}],
width: this.data.windowWidth,
height: this.data.windowHeight,
extra: {
radar: {
max: 150
}
}
});
},
getcomment : function () {
let params = { let params = {
url: 'coach/comment', url: 'coach/comment',
data:{ data: {
coach_id : 5, coach_id: this.data.coach_id,
class_id : this.data.class_id, class_id: this.data.class_id,
lesson_id : this.data.lesson_id, lesson_id: this.data.lesson_id,
child_id : this.data.child_id, child_id: this.data.child_id,
}, },
callback : (data) => { callback: (data) => {
console.log(data) console.log(data)
this.setData({ this.setData({
coach : data.coach, coach: data.coach,
coachcomment : data, coachcomment: data,
scoreList : data.comment_score, scoreList: data.comment_score,
}) })
var arrs = []; var arrs = [];
var newArrs =[]; var newArrs = [];
for(var i = 0 ; i<this.data.scoreList.length; i++) { for (var i = 0; i < this.data.scoreList.length; i++) {
// console.log(this.data.scoreList) let option = {
arrs.push(this.data.scoreList[i].comment_norm.name) name: this.data.scoreList[i].comment_norm.name + ' ' + this.data.scoreList[i].score,
// console.log(arrs) max: 100
}
arrs.push(option)
newArrs.push(this.data.scoreList[i].score) newArrs.push(this.data.scoreList[i].score)
// console.log(newArrs)
} }
this.setData({ this.getOption(arrs, newArrs)
arr1:arrs,
arr2: newArrs
})
console.log(this.data.arr1)
console.log(this.data.arr2)
this.getCharts()
}, },
} }
base.newRequest(params) base.newRequest(params)
}, },
getbadges : function () { getOption(xData, data_cur) {
var option = {
backgroundColor: "#ffffff",
color: ["#00E2BB"],
xAxis: {
show: false
},
yAxis: {
show: false
},
radar: {
shape: 'circle',
splitNumber: 4, // 分割段数
indicator: xData, // 数据
textStyle: {
borderColor: '#00E2BB',
borderWidth: 2
},
name: { // 文本样式
textStyle: {
color: '#1A1A1A',
fontSize: 12
}
},
axisLine: { //配置雷达图的射线样式颜色
lineStyle: {
color: '#E0E0E0',
width: 1
},
},
splitLine: { // 分割线样式
lineStyle: {
color: '#E0E0E0',
width: 1,
type: 'dashed'
},
},
splitArea: { // 分割区域样式
areaStyle: {
color: ['#fff']
}
},
},
series: [{
type: 'radar',
data: [{
value: data_cur
}],
itemStyle: { //此属性的颜色和下面areaStyle属性的颜色都设置成相同色即可实现
color: '#00E2BB',
borderColor: '#00E2BB'
},
lineStyle: { // 线条样式
width: 2
},
symbol: 'circle', // 拐点的样式,还可以取值'rect','angle'等
symbolSize: 4, // 拐点的大小
areaStyle: { // 覆盖区域颜色
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: '#00E24A'
},
{
offset: 1,
color: '#00E3BC' // 100% 处的颜色
}
]
}
},
}]
};
chartLine.setOption(option);
},
getbadges: function () {
let params = { let params = {
url: 'badges', url: 'badges',
data : { data: {
user_child_id : this.data.child_id, user_child_id: this.data.child_id,
class_id : this.data.class_id , class_id: this.data.class_id,
class_single_id : this.data.class_single_id, class_single_id: this.data.class_single_id,
}, },
callback : (data) =>{ callback: (data) => {
console.log(data)
this.setData({ this.setData({
awardList : data awardList: data
}) })
} }
} }
......
{ {
"usingComponents": {}, "usingComponents": {
"ec-canvas": "../../../ec-canvas/ec-canvas"
},
"navigationBarTitleText": "运动评价" "navigationBarTitleText": "运动评价"
} }
\ No newline at end of file
<view class="top"> <view class="top">
<view class="title">运动表现</view> <view class="title">运动表现</view>
<view class="canvasbox"> <view class="canvasbox">
<canvas canvas-id="radarCanvas" disable-scroll="true" class="canvas"></canvas> <ec-canvas id="mychart" class="canvas" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view> </view>
</view> </view>
<view class="one"> <view class="one">
......
.container {
width: 100%;
height: 500rpx;
}
ec-canvas {
width: 100%;
height: 100%;
}
page { page {
background: #F7F8FA; background: #F7F8FA;
padding: 20rpx 30rpx 60rpx 30rpx; padding: 20rpx 30rpx 60rpx 30rpx;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment