用html批量绘制k线图,k线图.html

该博客介绍了如何使用ECharts库创建一个动态的OHLC(开盘价、最高价、最低价、收盘价)烛形图,并结合了数据量展示、实时缩放和体积柱状图等功能。示例代码详细展示了配置项和数据生成方法,适用于数据可视化和金融图表的开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ECharts

// 基于准备好的dom,初始化echarts实例

var myChart = echarts.init(document.getElementById('main'));

//指定图表的配置项和数据

var upColor = '#ec0000';

var upBorderColor = '#8A0000';

var downColor = '#00da3c';

var downBorderColor = '#008F28';

var dataCount = 2e5;

var data = generateOHLC(dataCount);

var option = {

dataset: {

source: data

},

title: {

text: 'Data Amount: ' + echarts.format.addCommas(dataCount)

},

tooltip: {

trigger: 'axis',

axisPointer: {

type: 'line'

}

},

toolbox: {

feature: {

dataZoom: {

yAxisIndex: false

},

}

},

grid: [{

left: '10%',

right: '10%',

bottom: 200

},

{

left: '10%',

right: '10%',

height: 80,

bottom: 80

}

],

xAxis: [{

type: 'category',

scale: true,

boundaryGap: false,

// inverse: true,

axisLine: {

onZero: false

},

splitLine: {

show: false

},

splitNumber: 20,

min: 'dataMin',

max: 'dataMax'

},

{

type: 'category',

gridIndex: 1,

scale: true,

boundaryGap: false,

axisLine: {

onZero: false

},

axisTick: {

show: false

},

splitLine: {

show: false

},

axisLabel: {

show: false

},

splitNumber: 20,

min: 'dataMin',

max: 'dataMax'

}

],

yAxis: [{

scale: true,

splitArea: {

show: true

}

},

{

scale: true,

gridIndex: 1,

splitNumber: 2,

axisLabel: {

show: false

},

axisLine: {

show: false

},

axisTick: {

show: false

},

splitLine: {

show: false

}

}

],

dataZoom: [{

type: 'inside',

xAxisIndex: [0, 1],

start: 10,

end: 100

},

{

show: true,

xAxisIndex: [0, 1],

type: 'slider',

bottom: 10,

start: 10,

end: 100,

handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',

handleSize: '105%'

}

],

visualMap: {

show: false,

seriesIndex: 1,

dimension: 6,

pieces: [{

value: 1,

color: upColor

}, {

value: -1,

color: downColor

}]

},

series: [{

type: 'candlestick',

itemStyle: {

color: upColor,

color0: downColor,

borderColor: upBorderColor,

borderColor0: downBorderColor

},

encode: {

x: 0,

y: [1, 4, 3, 2]

}

},

{

name: 'Volumn',

type: 'bar',

xAxisIndex: 1,

yAxisIndex: 1,

itemStyle: {

color: '#7fbe9e'

},

large: true,

encode: {

x: 0,

y: 5

}

}

]

};

function generateOHLC(count) {

var data = [];

var xValue = +new Date(2011, 0, 1);

var minute = 60 * 1000;

var baseValue = Math.random() * 12000;

var boxVals = new Array(4);

var dayRange = 12;

for (var i = 0; i < count; i++) {

baseValue = baseValue + Math.random() * 20 - 10;

for (var j = 0; j < 4; j++) {

boxVals[j] = (Math.random() - 0.5) * dayRange + baseValue;

}

boxVals.sort();

var idxRandom = Math.random();

var openIdx = Math.round(Math.random() * 3);

var closeIdx = Math.round(Math.random() * 2);

if (closeIdx === openIdx) {

closeIdx++;

}

var volumn = boxVals[3] * (1000 + Math.random() * 500);

// ['open', 'close', 'lowest', 'highest', 'volumn']

// [1, 4, 3, 2]

data[i] = [

echarts.format.formatTime('yyyy-MM-dd\nhh:mm:ss', xValue += minute),

+boxVals[openIdx].toFixed(2), // open

+boxVals[3].toFixed(2), // highest

+boxVals[0].toFixed(2), // lowest

+boxVals[closeIdx].toFixed(2), // close

volumn.toFixed(0),

getSign(data, i, +boxVals[openIdx], +boxVals[closeIdx], 4) // sign

];

}

return data;

function getSign(data, dataIndex, openVal, closeVal, closeDimIdx) {

var sign;

if (openVal > closeVal) {

sign = -1;

} else if (openVal < closeVal) {

sign = 1;

} else {

sign = dataIndex > 0

// If close === open, compare with close of last record

?

(data[dataIndex - 1][closeDimIdx] <= closeVal ? 1 : -1)

// No record of previous, set to be positive

:

1;

}

return sign;

}

}

// 使用刚指定的配置项和数据显示图表。

myChart.setOption(option);

一键复制

编辑

Web IDE

原始数据

按行查看

历史

html5和c++开源 K线图工具, Create interactive charts easily for your web projects. Used by tens of thousands of developers and 61 out of the world's 100 largest companies, Highcharts is the simplest yet most flexible charting API on the market. Read more » Download » TemperatureRainfallTokyo climateSunshine hoursJanFebMarAprMayJunJulAugSepOctNovDec5°C10°C15°C20°C25°C30°C0 mm50 mm100 mm150 mm200 mm250 mmHighcharts.com Highstock Highstock lets you create stock or general timeline charts in pure JavaScript. Including sophisticated navigation options like a small navigator series, preset date ranges, date picker, scrolling and panning. Read more » Download » ZoomUSD to EUR14. Feb21. Feb28. Feb7. Mar14. Mar21. Mar28. Mar4. Apr11. Apr18. Apr25. Apr2. May9. May20042006200820100.70.650.751m3m6mYTD1yAllHighcharts.com Highmaps Interactive map charts with drilldown and touch support. Build interactive maps to display sales, election results or any other information linked to geography. Perfect for standalone use or in dashboards in combination with Highcharts! Read more » Download » Highcharts © Natural Earth Highcharts Cloud Online charts for non-techies. Create smashing, interactive diagrams for your news site or blog, or for sharing with your friends on social media. Read more » Highcharts Cloud 1 2 3 4 Free for non-commercial Do you want to use Highcharts for a personal website, a school site or a non-profit organisation? Then you don't need our permission, just go on! HTML 5 Based on native browser technologies, no plugins needed Open Fork us on GitHub and participate in tech discussions Compatible With modern browsers including mobile, tablets and old IE back to IE6 Dynamic Add, remove and modify series and points or modify axes Any chart you'd like Highcharts supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerang
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值