公司有个项目,需要用angularjs和Echarts生成K线图,点击4个按钮,来切换数据,显示各个状态下的图表(卡住了)。
对angularjs不是很懂,希望大家能帮忙看
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
</head>
<style>
.chartsBtn{
background: none;
border: 1px solid #999;
width: 45px;
height: 23px;
position: relative;
left: 10%;
top: 50px;
cursor: pointer;
z-index: 999999999;
}
.chartsBtn:hover{
background: #2f5895;
color: #fff;
outline: none
}
.chartsBtnClick{
background: #2f5895;
color: #fff;
outline: none
}
</style>
<script src="/_Global/Angular/angular-1.6.8/angular.js"></script>
<script src="/_Global/Echarts/Js/echarts.js"></script>
<script type="text/javascript">
change(1)
function change(type){
//创建一个模块
var app = angular.module('app', []);
//添加控制器
app.controller('lineCtrl', function($scope) {
//时间,开盘价,收盘价,交易最低价,交易最高价(日K)
var data0 = splitData([
['2013/1/24 13:01', 2320.26,2320.26,2287.3,2362.94],
['2013/1/24 13:02', 2300,2291.3,2288.26,2308.38],
['2013/1/24 13:03', 2295.35,2346.5,2295.35,2346.92],
['2013/1/24 13:04', 2347.22,2358.98,2337.35,2363.8],
['2013/1/24 13:05', 2360.75,2382.48,2347.89,2383.76],
['2013/1/24 13:06', 2383.43,2385.42,2371.23,2391.82],
['2013/1/24 13:07', 2377.41,2419.02,2369.57,2421.15],
['2013/1/24 13:08', 2425.92,2428.15,2417.58,2440.38],
['2013/1/24 13:09', 2411,2433.13,2403.3,2437.42],
['2013/1/24 13:10', 2432.68,2434.48,2427.7,2441.73],
['2013/1/24 13:11', 2430.69,2418.53,2394.22,2433.89],
['2013/1/24 13:12', 2416.62,2432.4,2414.4,2443.03],
['2013/1/24 13:13', 2441.91,2421.56,2415.43,2444.8],
['2013/1/24 13:14', 2420.26,2382.91,2373.53,2427.07],
['2013/1/24 13:15', 2383.49,2397.18,2370.61,2397.94],
['2013/1/24 13:16', 2378.82,2325.95,2309.17,2378.82],
['2013/1/24 13:17', 2322.94,2314.16,2308.76,2330.88],
['2013/1/24 13:18', 2320.62,2325.82,2315.01,2338.78],
['2013/1/24 13:19', 2313.74,2293.34,2289.89,2340.71],
['2013/1/24 13:20', 2297.77,2313.22,2292.03,2324.63],
['2013/1/24 13:21', 2322.32,2365.59,2308.92,2366.16],
['2013/1/24 13:22', 2364.54,2359.51,2330.86,2369.65],
['2013/1/24 14:28', 2190.1,2148.35,2126.22,2190.1]
]);
//切割数组,把数组中的日期和数据分离,返回数组中的日期和数据
function splitData(rawData) {
var categoryData = [];//X轴时间
var values = [];
for (var i = 0; i < rawData.length; i++) {
categoryData.push(rawData[i].splice(0, 1)[0]);
//数据数组,即数组中除日期外的数据
values.push(rawData[i])
}
return {
categoryData: categoryData, //X轴数据
values: values, //Y轴数据
data0:data0
};
}
$scope.legend = ['日K', 'MA5', 'MA10', 'MA20','O','C','L','H'];
$scope.item = data0.categoryData;
$scope.data = data0.values;
$scope.data0 = data0;
});
app.directive('line', function() {
return {
scope: {
id: "@",
legend: "=",
item: "=",
data: "="
},
restrict: 'E',
template: '<div style="height:900px;width:1800px"></div>',
replace: true,
link:function($scope, element, attrs, controller) {
//计算MA平均线,N日移动平均线=N日收盘价之和/N dayCount要计算的天数(5,10,20,30)
function calculateMA(dayCount) {
var result = [];
for (var i = 0, len = $scope.data.length; i < len; i++) {
if (i < dayCount) {
result.push('-');
continue; //结束单次循环,即不输出本次结果
}
var sum = 0;
for (var j = 0; j < dayCount; j++) {
//收盘价总和
sum += $scope.data[i - j][1];
}
result.push(sum / dayCount);
}
return result;
};
function splitData(rawData) {
var categoryData = [];//X轴时间
var values = [];
for (var i = 0; i < rawData.length; i++) {
categoryData.push(rawData[i].splice(0, 1)[0]);
//数据数组,即数组中除日期外的数据
values.push(rawData[i])
}
return {
categoryData: categoryData, //X轴数据
values: values, //Y轴数据
data0:data0
};
}
function fntip(a){
var tipArry=[];
for (var i = 0, len = $scope.data.length; i < len; i++) {
tipArry.push($scope.data[i][a])
}
return tipArry;
};
var option = {
title: { //标题
text: '上证指数',
left: 0
},
// 提示框,鼠标悬浮交互时的信息提示
tooltip: { //提示框
trigger: 'axis', //触发类型:坐标轴触发
axisPointer: { //坐标轴指示器配置项
type: 'cross' //指示器类型,十字准星
},
formatter: function (params,ticket,callback) {
var res = params[0].name;
for (var i = 0, l = params.length; i < l; i++) {
if(params[i].seriesName=="日K"){
continue
}
res += '<br/>' +params[i].marker+params[i].seriesName + ' : ' + params[i].value;
}
return res
}
},
// 图例
legend: {
data: $scope.legend
},
grid: { //直角坐标系
show:true,
left: '10%', //图表在窗口的位置
right: '10%',
bottom: '15%',
//backgroundColor:'#ccc'//图表背景色
},
// 横轴坐标轴
xAxis: [{
type: 'category', //坐标轴类型,类目轴
//scale: true, //只在数字轴中有效
boundaryGap : false, //刻度作为分割线,标签和数据点会在两个刻度上
axisLine: {onZero: false},
splitLine: {show: false}, //是否显示坐标轴轴线
//splitNumber: 20, //坐标轴的分割段数,预估值,在类目轴中无效
min: 'dataMin', //特殊值,数轴上的最小值作为最小刻度
max: 'dataMax', //特殊值,数轴上的最大值作为最大刻度
data: $scope.item
}],
// 纵轴坐标轴
yAxis: [{
scale: true, //坐标刻度不强制包含零刻度
splitArea: {
show: true //背景色间隔显示
}
}],
dataZoom: [ //用于区域缩放
{
filterMode:'filter', //当前数据窗口外的数据被过滤掉来达到数据窗口缩放的效果 默认值filter
type: 'inside', //内置型数据区域缩放组件
start: 50, //数据窗口范围的起始百分比
end: 100 //数据窗口范围的结束百分比
},
{
show: true,
type: 'slider', //滑动条型数据区域缩放组件
y: '90%',
start: 50,
end: 100
}
],
// 数据内容数组
series:[ //图表类型
{
name: '日K',
type: 'candlestick', //K线图
data: $scope.data, //y轴对应的数据
markPoint: { //图表标注
label: { //标注的文本
normal: { //默认不显示标注
show:true,
// position:['20%','30%'],//标记文字的位置
formatter: function (param) { //标签内容控制器
return param != null ? Math.round(param.value) : '';
},
//color:"#000"//标注的文字颜色
}
}
},
/////////////////////////////////图标标线///////////////////////////
markLine: {
symbol: ['none', 'none'],
data: [
{
type: 'min',//最低收盘价辅助线
valueDim: 'close'
},
{
type: 'max',//最高收盘价辅助线
valueDim: 'close'
}
]
}
},
{ //MA5 5天内的收盘价之和/5
name: 'MA5',
type: 'line',
data: calculateMA(5),
smooth: true,
lineStyle: {
normal: {opacity: 0.5}
}
},
{
name: 'MA10',
type: 'line',
data: calculateMA(10),
smooth: true,
lineStyle: { //标线的样式
normal: {opacity: 0.5}
}
},
{
name: 'MA20',
type: 'line',
data: calculateMA(20),
smooth: true,
lineStyle: {
normal: {opacity: 0.5}
}
},
{
name: 'O',
type: 'line',
data: fntip(0),
smooth: true,
symbolSize: 0, // symbol的大小设置为0
showSymbol: false, // 不显示symbol
lineStyle: {
width: 0, // 线宽是0
color: 'rgba(0, 0, 0, 0)' // 线的颜色是透明的
}
},
{
name: 'C',
type: 'line',
data: fntip(1),
smooth: true,
symbolSize: 0, // symbol的大小设置为0
showSymbol: false, // 不显示symbol
lineStyle: {
width: 0, // 线宽是0
color: 'rgba(0, 0, 0, 0)' // 线的颜色是透明的
}
},
{
name: 'L',
type: 'line',
data: fntip(2),
smooth: true,
symbolSize: 0, // symbol的大小设置为0
showSymbol: false, // 不显示symbol
lineStyle: {
width: 0, // 线宽是0
color: 'rgba(0, 0, 0, 0)' // 线的颜色是透明的
}
},
{
name: 'H',
type: 'line',
data: fntip(3),
smooth: true,
symbolSize: 0, // symbol的大小设置为0
showSymbol: false, // 不显示symbol
lineStyle: {
width: 0, // 线宽是0
color: 'rgba(0, 0, 0, 0)' // 线的颜色是透明的
}
}
]
};
//macarons 为图表主题
var myChart = echarts.init(document.getElementById($scope.id),'macarons');
myChart.setOption(option);
}
};
});
}
</script>
<body ng-app="app" ng-controller="lineCtrl">
<button class="chartsBtn" ng-click="change(1)">1Min</button>
<button class="chartsBtn" ng-click="change(2)">3Min</button>
<button class="chartsBtn" ng-click="change(3)">5Min</button>
<button class="chartsBtn" ng-click="change(4)">15Min</button>
<line id="main" legend="legend" item="item" data="data"></line>
</body>
</html>
没用过angular 不过根据使用vue的经验来说 可以使用再次封装过的插件https://github.com/wangshijun...,每次点击直接修改数据,可以自动进行双向数据绑定。