vue引入echarts 展示散点图
APP,H5,三端通用
组件封装版:
视图
XML/HTML Code复制内容到剪贴板
- <template>
- <view>
- <view class="n-position-relative" style="width: 100%; height: 800rpx;">
- <charts1 ref="charts1" :list="charts1.list" :count="charts1.count" :colors="charts1.colors" :labels="charts1.labels" @click="handleClick"></charts1>
- </view>
- </view>
- </template>
- <script>
- import charts1 from '@/components/l-echart/charts/ecStatClustering.vue'
- export default {
- components: {
- charts1
- },
- data() {
- return {
- charts1:{
- count:4, // 分成四个指标
- colors:[
- '#37A2DA',
- '#37a354',
- '#e06343',
- '#b5bd48',
- ],
- labels:[
- '正面-高关注',
- '正面-低关注',
- '负面-高关注',
- '负面-低关注',
- ],
- list:[
- [3.275154, 2.957587],
- [-3.344465, 2.603513],
- [0.355083, 3.376585],
- [1.852435, 3.547351],
- [-2.078973, 2.552013],
- [-0.993756, 0.884433],
- [2.682252, 4.007573],
- [-3.087776, 2.878713],
- [-1.565978, 1.256985],
- [2.441611, 0.444826],
- [-0.659487, 3.111284],
- [-0.459601, 2.618005],
- [2.17768, 2.387793],
- [-2.920969, 2.917485],
- [-0.028814, 4.168078],
- [3.625746, 2.119041],
- [-3.912363, 1.325108],
- [-0.551694, 2.814223],
- [2.855808, 3.483301],
- [-3.594448, 2.856651],
- [0.421993, 2.372646],
- [1.650821, 3.407572],
- [-2.082902, 3.384412],
- [-0.718809, 2.492514],
- [4.513623, 3.841029],
- [-4.822011, 4.607049],
- [-0.656297, 1.449872],
- [1.919901, 4.439368],
- [-3.287749, 3.918836],
- [-1.576936, 2.977622],
- [3.598143, 1.97597],
- [-3.977329, 4.900932],
- [-1.79108, 2.184517],
- [3.914654, 3.559303],
- [-1.910108, 4.166946],
- [-1.226597, 3.317889],
- [1.148946, 3.345138],
- [-2.113864, 3.548172],
- [0.845762, 3.589788],
- [2.629062, 3.535831],
- ]
- }
- }
- },
- onLoad() {
- this.init()
- },
- methods: {
- init() {
- setTimeout(() => {
- this.$refs.charts1.init()
- }, 100);
- },
- handleClick(params) {
- uni.navigateTo({
- url: '/pages/list/hte2',
- success: res => {},
- fail: () => {},
- complete: () => {}
- });
- }
- }
- }
- </script>
- <style>
- </style>
组件内容:
XML/HTML Code复制内容到剪贴板
- <template>
- <view class="charts-box">
- <l-echart ref="chart"></l-echart>
- </view>
- </template>
- <script>
- import lecharts from '@/components/l-echart/l-echart.vue'
- import * as echarts from '@/components/l-echart/echarts.js';
- import ecStat from 'echarts-stat';
- export default {
- name:"ecStatClustering",
- components: {
- lecharts
- },
- props:{
- list:{
- type:Array,
- default:[]
- },
- count:{
- type:Number,
- default:4
- },
- colors:{
- type:Array,
- default:[]
- },
- labels:{
- type:Array,
- default:[]
- }
- },
- data() {
- return {
- };
- },
- methods:{
- init(){
- this.initChart()
- },
- initChart(){
- // y轴没负数
- let data = this.list;
- var CLUSTER_COUNT = this.count;
- var DIENSIION_CLUSTER_INDEX = 2;
- var COLOR_ALL = this.colors;
- var COLOR_TXT = this.labels
- var pieces = [];
- for (var i = 0; i < CLUSTER_COUNT; i++) {
- pieces.push({
- "value": i,
- "label": COLOR_TXT[i],
- "color": COLOR_ALL[i],
- "symbol": 'circle',
- "symbolSize": 10
- });
- }
- // visualMap参数参照https://blog.csdn.net/qq_42363090/article/details/105245615
- // markLine基线参数参照https://blog.csdn.net/qq_42177730/article/details/81223666
- let chartOptions = {
- dataset: [{
- source: data
- },
- {
- transform: {
- type: 'ecStat:clustering',
- // print: true,
- config: {
- clusterCount: CLUSTER_COUNT,
- outputType: 'single',
- outputClusterIndexDimension: DIENSIION_CLUSTER_INDEX
- }
- }
- }
- ],
- tooltip: {
- position: 'top'
- },
- visualMap: {
- type: 'piecewise',
- top: 'bottom',
- orient:"horizontal",// 放置 visualMap 组件,水平('horizontal')或者竖直('vertical')。
- min: 0,
- max: CLUSTER_COUNT,
- left: 'center', // 组件离容器左侧的距离,'left', 'center', 'right','20%'
- splitNumber: CLUSTER_COUNT,
- dimension: DIENSIION_CLUSTER_INDEX,
- pieces: pieces,
- // itemWidth:10,
- // itemGap:5,
- // padding:[20, 1] ,// 内边距,单位px,默认各方向内边距为5,接受数组分别设定上右下左边距
- textStyle:{
- fontSize:10
- }
- },
- grid: {
- left: 20 ,// grid 组件离容器左侧的距离。可以是像 '20%' 也可以是 'left', 'center', 'right'。
- right: 40
- },
- xAxis: {},
- yAxis: {
- offset:-140
- },
- series: {
- type: 'scatter',
- encode: {
- tooltip: [0, 1]
- },
- symbolSize: 15,
- itemStyle: {
- borderColor: '#555'
- },
- datasetIndex: 1,
- markLine: {
- silent: true, // 图形是否不响应和触发鼠标事件,默认为 false,即响应和触发鼠标事件
- // symbol:'none',// 去掉基线箭头
- lineStyle: {
- normal: {
- color: '#e06343' // 这儿设置安全基线颜色
- },
- },
- data: [
- {
- yAxis: 2.5
- }
- ],
- label: {
- normal: {
- formatter: '标准\n基线', // 这儿设置安全基线
- color: '#e06343'
- }
- },
- }
- }
- };
- const _this = this
- // console.log(chartOptions)
- echarts.registerTransform(ecStat.transform.clustering);
- this.$refs.chart.init(config => {
- const {
- canvas
- } = config;
- const chart = echarts.init(canvas, null, config);
- canvas.setChart(chart);
- chart.setOption(chartOptions);
- chart.on('click',(params) => {
- this.$emit("click",params)
- });
- // 需要把 chart 返回
- return chart;
- });
- }
- }
- }
- </script>
- <style scoped>
- .charts-box{
- width: 100%;
- height: 100%;
- }
- </style>
1、引入组件,放在components目录下
2、引入stat
XML/HTML Code复制内容到剪贴板
- npm install echarts-stat -S
3、vue页面:
XML/HTML Code复制内容到剪贴板
- <template>
- <view>
- <view class="n-position-relative" style="width: 100%; height: 800rpx;">
- <l-echart ref="chart"></l-echart>
- </view>
- </view>
- </template>
- <script>
- import lecharts from '@/components/l-echart/l-echart.vue'
- import * as echarts from '@/components/l-echart/echarts.js';
- import ecStat from 'echarts-stat';
- export default {
- components: {
- lecharts
- },
- data() {
- return {
- }
- },
- onLoad() {
- this.init()
- },
- methods: {
- init() {
- setTimeout(e => {
- this.initChart()
- }, 200);
- },
- initChart() {
- let data = [
- [3.275154, 2.957587],
- [-3.344465, 2.603513],
- [0.355083, -3.376585],
- [1.852435, 3.547351],
- [-2.078973, 2.552013],
- [-0.993756, -0.884433],
- [2.682252, 4.007573],
- [-3.087776, 2.878713],
- [-1.565978, -1.256985],
- [2.441611, 0.444826],
- [-0.659487, 3.111284],
- [-0.459601, -2.618005],
- [2.17768, 2.387793],
- [-2.920969, 2.917485],
- [-0.028814, -4.168078],
- [3.625746, 2.119041],
- [-3.912363, 1.325108],
- [-0.551694, -2.814223],
- [2.855808, 3.483301],
- [-3.594448, 2.856651],
- [0.421993, -2.372646],
- [1.650821, 3.407572],
- [-2.082902, 3.384412],
- [-0.718809, -2.492514],
- [4.513623, 3.841029],
- [-4.822011, 4.607049],
- [-0.656297, -1.449872],
- [1.919901, 4.439368],
- [-3.287749, 3.918836],
- [-1.576936, -2.977622],
- [3.598143, 1.97597],
- [-3.977329, 4.900932],
- [-1.79108, -2.184517],
- [3.914654, 3.559303],
- [-1.910108, 4.166946],
- [-1.226597, -3.317889],
- [1.148946, 3.345138],
- [-2.113864, 3.548172],
- [0.845762, -3.589788],
- [2.629062, 3.535831],
- [-1.640717, 2.990517],
- [-1.881012, -2.485405],
- [4.606999, 3.510312],
- [-4.366462, 4.023316],
- [0.765015, -3.00127],
- [3.121904, 2.173988],
- [-4.025139, 4.65231],
- [-0.559558, -3.840539],
- [4.376754, 4.863579],
- [-1.874308, 4.032237],
- [-0.089337, -3.026809],
- [3.997787, 2.518662],
- [-3.082978, 2.884822],
- [0.845235, -3.454465],
- [1.327224, 3.358778],
- [-2.889949, 3.596178],
- [-0.966018, -2.839827],
- [2.960769, 3.079555],
- [-3.275518, 1.577068],
- [0.639276, -3.41284]
- ];
- var CLUSTER_COUNT = 3;
- var DIENSIION_CLUSTER_INDEX = 2;
- var COLOR_ALL = [
- '#37A2DA',
- '#e06343',
- '#37a354',
- // '#b55dba',
- // '#b5bd48',
- // '#8378EA',
- // '#96BFFF'
- ];
- // var COLOR_TXT = [
- // '热度',
- // '正面',
- // '负面'
- // ]
- var pieces = [];
- for (var i = 0; i < CLUSTER_COUNT; i++) {
- pieces.push({
- "value": i,
- "label": 'cluster ' + i,
- "color": COLOR_ALL[i]
- });
- }
- // visualMap参数参照https://blog.csdn.net/qq_42363090/article/details/105245615
- let chartOptions = {
- dataset: [{
- source: data
- },
- {
- transform: {
- type: 'ecStat:clustering',
- // print: true,
- config: {
- clusterCount: CLUSTER_COUNT,
- outputType: 'single',
- outputClusterIndexDimension: DIENSIION_CLUSTER_INDEX
- }
- }
- }
- ],
- tooltip: {
- position: 'top'
- },
- visualMap: {
- type: 'piecewise',
- top: 'bottom',
- orient:"horizontal",// 放置 visualMap 组件,水平('horizontal')或者竖直('vertical')。
- min: 0,
- max: CLUSTER_COUNT,
- left: 'center', // 组件离容器左侧的距离,'left', 'center', 'right','20%'
- splitNumber: CLUSTER_COUNT,
- dimension: DIENSIION_CLUSTER_INDEX,
- pieces: pieces,
- },
- grid: {
- left: 30 ,// grid 组件离容器左侧的距离。可以是像 '20%' 也可以是 'left', 'center', 'right'。
- right: 20
- },
- xAxis: {},
- yAxis: {},
- series: {
- type: 'scatter',
- encode: {
- tooltip: [0, 1]
- },
- symbolSize: 15,
- itemStyle: {
- borderColor: '#555'
- },
- datasetIndex: 1
- }
- };
- const _this = this
- // console.log(chartOptions)
- echarts.registerTransform(ecStat.transform.clustering);
- this.$refs.chart.init(config => {
- const {
- canvas
- } = config;
- const chart = echarts.init(canvas, null, config);
- // console.log(chart,chartOptions)
- canvas.setChart(chart);
- chart.setOption(chartOptions);
- // .params.name
- chart.on('click',function(params){
- console.log(params)
- uni.navigateTo({
- url: '/pages/list/hte2',
- success: res => {},
- fail: () => {},
- complete: () => {}
- });
- // const currentChartsData = bubbleOptions.series[0].data[params.dataIndex]
- // const currentTag = _this.tags[params.dataIndex]
- // const index = graceJS.arrayIndexOf(_this.checktags, currentTag.id)
- // // console.log(graceJS.arrayIndexOf(_this.tags, currentTag.id))
- // if(index == -1){
- // // 不存在,加一组数组
- // _this.checktags.push(currentTag.id);
- // currentChartsData.itemStyle.normal.color = "#cdb659"
- // }else{
- // // 已经存在
- // graceJS.arrayDrop(_this.checktags, index);
- // currentChartsData.itemStyle.normal.color = "#7b7a7a"
- // }
- // chart.setOption(bubbleOptions);
- });
- // 需要把 chart 返回
- return chart;
- });
- }
- }
- }
- </script>
- <style>
- </style>
自定义标记类型
ECharts 提供的标记类型包括
'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。
XML/HTML Code复制内容到剪贴板
- <template>
- <view>
- <view class="n-position-relative" style="width: 100%; height: 800rpx;">
- <l-echart ref="chart"></l-echart>
- </view>
- </view>
- </template>
- <script>
- import lecharts from '@/components/l-echart/l-echart.vue'
- import * as echarts from '@/components/l-echart/echarts.js';
- import ecStat from 'echarts-stat';
- export default {
- components: {
- lecharts
- },
- data() {
- return {
- }
- },
- onLoad() {
- this.init()
- },
- methods: {
- init() {
- setTimeout(e => {
- this.initChart()
- }, 200);
- },
- initChart() {
- // y轴没负数
- let data = [
- [3.275154, 2.957587],
- [-3.344465, 2.603513],
- [0.355083, 3.376585],
- [1.852435, 3.547351],
- [-2.078973, 2.552013],
- [-0.993756, 0.884433],
- [2.682252, 4.007573],
- [-3.087776, 2.878713],
- [-1.565978, 1.256985],
- [2.441611, 0.444826],
- [-0.659487, 3.111284],
- [-0.459601, 2.618005],
- [2.17768, 2.387793],
- [-2.920969, 2.917485],
- [-0.028814, 4.168078],
- [3.625746, 2.119041],
- [-3.912363, 1.325108],
- [-0.551694, 2.814223],
- [2.855808, 3.483301],
- [-3.594448, 2.856651],
- [0.421993, 2.372646],
- [1.650821, 3.407572],
- [-2.082902, 3.384412],
- [-0.718809, 2.492514],
- [4.513623, 3.841029],
- [-4.822011, 4.607049],
- [-0.656297, 1.449872],
- [1.919901, 4.439368],
- [-3.287749, 3.918836],
- [-1.576936, 2.977622],
- [3.598143, 1.97597],
- [-3.977329, 4.900932],
- [-1.79108, 2.184517],
- [3.914654, 3.559303],
- [-1.910108, 4.166946],
- [-1.226597, 3.317889],
- [1.148946, 3.345138],
- [-2.113864, 3.548172],
- [0.845762, 3.589788],
- [2.629062, 3.535831],
- // [-1.640717, 2.990517],
- // [-1.881012, -2.485405],
- // [4.606999, 3.510312],
- // [-4.366462, 4.023316],
- // [0.765015, -3.00127],
- // [3.121904, 2.173988],
- // [-4.025139, 4.65231],
- // [-0.559558, -3.840539],
- // [4.376754, 4.863579],
- // [-1.874308, 4.032237],
- // [-0.089337, -3.026809],
- // [3.997787, 2.518662],
- // [-3.082978, 2.884822],
- // [0.845235, -3.454465],
- // [1.327224, 3.358778],
- // [-2.889949, 3.596178],
- // [-0.966018, -2.839827],
- // [2.960769, 3.079555],
- // [-3.275518, 1.577068],
- // [0.639276, -3.41284]
- ];
- var CLUSTER_COUNT = 4;
- var DIENSIION_CLUSTER_INDEX = 2;
- var COLOR_ALL = [
- '#37A2DA',
- '#37a354',
- '#e06343',
- '#b5bd48',
- ];
- var COLOR_TXT = [
- '热度',
- '正面',
- '负面',
- '特定公司'
- ]
- var pieces = [];
- for (var i = 0; i < CLUSTER_COUNT; i++) {
- if(i == 3){
- pieces.push({
- "value": i,
- "label": COLOR_TXT[i],
- "color": COLOR_ALL[i],
- // "symbol": 'arrow',
- "symbol": 'image://https://demo13-liqinwl.oss-cn-beijing.aliyuncs.com/uniapp/star.png',
- "symbolSize": 20
- });
- }else{
- pieces.push({
- "value": i,
- "label": COLOR_TXT[i],
- "color": COLOR_ALL[i],
- "symbol": 'circle',
- "symbolSize": 10
- });
- }
- }
- // 'circle', 'rect’, 'roundRect’, 'triangle’, 'diamond’, 'pin’, 'arrow'
- // pieces[2].symbol = 'arrow'
- // visualMap参数参照https://blog.csdn.net/qq_42363090/article/details/105245615
- let chartOptions = {
- dataset: [{
- source: data
- },
- {
- transform: {
- type: 'ecStat:clustering',
- // print: true,
- config: {
- clusterCount: CLUSTER_COUNT,
- outputType: 'single',
- outputClusterIndexDimension: DIENSIION_CLUSTER_INDEX
- }
- }
- }
- ],
- tooltip: {
- position: 'top'
- },
- visualMap: {
- type: 'piecewise',
- top: 'bottom',
- orient:"horizontal",// 放置 visualMap 组件,水平('horizontal')或者竖直('vertical')。
- min: 0,
- max: CLUSTER_COUNT,
- left: 'center', // 组件离容器左侧的距离,'left', 'center', 'right','20%'
- splitNumber: CLUSTER_COUNT,
- dimension: DIENSIION_CLUSTER_INDEX,
- pieces: pieces,
- },
- grid: {
- left: 30 ,// grid 组件离容器左侧的距离。可以是像 '20%' 也可以是 'left', 'center', 'right'。
- right: 20
- },
- xAxis: {},
- yAxis: {},
- series: {
- type: 'scatter',
- encode: {
- tooltip: [0, 1]
- },
- symbolSize: 15,
- itemStyle: {
- borderColor: '#555'
- },
- datasetIndex: 1
- }
- };
- const _this = this
- console.log(chartOptions)
- echarts.registerTransform(ecStat.transform.clustering);
- this.$refs.chart.init(config => {
- const {
- canvas
- } = config;
- const chart = echarts.init(canvas, null, config);
- // console.log(chart,chartOptions)
- canvas.setChart(chart);
- chart.setOption(chartOptions);
- // .params.name
- chart.on('click',function(params){
- console.log(params)
- uni.navigateTo({
- url: '/pages/list/hte2',
- success: res => {},
- fail: () => {},
- complete: () => {}
- });
- // const currentChartsData = bubbleOptions.series[0].data[params.dataIndex]
- // const currentTag = _this.tags[params.dataIndex]
- // const index = graceJS.arrayIndexOf(_this.checktags, currentTag.id)
- // // console.log(graceJS.arrayIndexOf(_this.tags, currentTag.id))
- // if(index == -1){
- // // 不存在,加一组数组
- // _this.checktags.push(currentTag.id);
- // currentChartsData.itemStyle.normal.color = "#cdb659"
- // }else{
- // // 已经存在
- // graceJS.arrayDrop(_this.checktags, index);
- // currentChartsData.itemStyle.normal.color = "#7b7a7a"
- // }
- // chart.setOption(bubbleOptions);
- });
- // 需要把 chart 返回
- return chart;
- });
- }
- }
- }
- </script>
- <style>
- </style>
上一篇 小程序-微信支付
下一篇 轮播图自动渐隐渐现,最后进入首页