百度echarts 怎么将字符串 JSON转换成符合格式的数据,图形

让人想犯罪 __ 提交于 2020-08-17 16:23:02
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('线状图')" />
</head>
<body class="gray-bg">
   <span id="sparkline19"></span> 
   <span class="line" id="sparkline201"></span>
   
     <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" class="line" ></div>
    
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: sparkline-js" /> 
	  <script th:inline="javascript">	  
	   
       

	    $(function () {  
		
		var id = [[${id}]] 
       $.ajax({
        type: "post",
        url: ctx + "user/mapping",
        data: {
           
        },
        success: function(r) {
		//alert(r)   
		var str = [];
		var str2 = [];
		var str3 = [];
		var str4  = [];
		
		console.log(r.data);
		str=JSON.parse("[" + r.data + "]");
		str3=r.data;
		str4=r.data;
		
		console.log(str4);
		for (i=0;i<str.length;i++ )
		{
		str2.push(str[i]);
		}
		console.log(str2); 
		
		      var mapResult2 = str2.map(function(item,index,array){
				return item-0;
				});
			 
		
		
		   $("#sparkline9").sparkline(mapResult2, {
	            type: 'line',
	            lineWidth: 1,
	            height: '300px',
	            lineColor: '#17997f',
	            fillColor: '#ffffff',
	        }); 
			$("#sparkline19").sparkline([mapResult2], {
	            type: 'line',
	            lineWidth: 1,
	            height: '300px',
	            lineColor: '#17997f',
	            fillColor: '#ffffff',
	        }); 
			 // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

        // 指定图表的配置项和数据
        var option = {
    title: {
        text: '折线图堆叠'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['邮件营销', '联盟广告', '视频广告']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: []
    },
    yAxis: {
        type: 'value'
    },
    series: [
        {
            name: '邮件营销',
            type: 'line',
            stack: '总量',
            data: [mapResult2]
        },       {
            name: '邮件营销11',
            type: 'line',
            stack: '总量',
            data: mapResult2
        }  
    ]
	
	
		
        }
		  // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option); 
		
		
    } 
	     
})

      
			
	    });
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('线状图')" />
</head>
<body class="gray-bg">
   <span id="sparkline19"></span> 
   <span class="line" id="sparkline201"></span>
   
     <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" class="line" ></div>
    
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: sparkline-js" /> 
	  <script th:inline="javascript">	  
	   
       

	    $(function () {  
		
		var id = [[${id}]] 
       $.ajax({
        type: "post",
        url: ctx + "user/mapping",
        data: {
           
        },
        success: function(r) {
		//alert(r)   
		var str = [];
		var str2 = [];
		var str3 = [];
		var str4  = [];
		
		console.log(r.data);
		str=JSON.parse("[" + r.data + "]");
		str3=r.data;
		str4=r.data;
		
		console.log(str4);
		for (i=0;i<str.length;i++ )
		{
		str2.push(str[i]);
		}
		console.log(str2); 
		
		      var mapResult2 = str2.map(function(item,index,array){
				return item-0;
				});
			 
		
		
		   $("#sparkline9").sparkline(mapResult2, {
	            type: 'line',
	            lineWidth: 1,
	            height: '300px',
	            lineColor: '#17997f',
	            fillColor: '#ffffff',
	        }); 
			$("#sparkline19").sparkline([mapResult2], {
	            type: 'line',
	            lineWidth: 1,
	            height: '300px',
	            lineColor: '#17997f',
	            fillColor: '#ffffff',
	        }); 
			 // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

        // 指定图表的配置项和数据
        var option = {
    title: {
        text: '折线图堆叠'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['邮件营销', '联盟广告', '视频广告']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: []
    },
    yAxis: {
        type: 'value'
    },
    series: [
        {
            name: '邮件营销',
            type: 'line',
            stack: '总量',
            data: [mapResult2]
        },       {
            name: '邮件营销11',
            type: 'line',
            stack: '总量',
            data: mapResult2
        }  
    ]
	
	
		
        }
		  // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option); 
		
		
    } 
	     
})

      
			
	    });
    </script>
</body>
</html>

  关键处理代码

 

 var mapResult2 = str2.map(function(item,index,array){
				return item-0;
				});

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!