From dc80dab1ed0d6b203a6a2e8dd1fe52e5a3651b09 Mon Sep 17 00:00:00 2001 From: yiliuyan Date: Wed, 10 Jan 2024 10:34:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96k=E7=BA=BF=E5=9B=BE=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chartspy/express/echarts.py | 115 ++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/chartspy/express/echarts.py b/chartspy/express/echarts.py index 944889b..683742b 100644 --- a/chartspy/express/echarts.py +++ b/chartspy/express/echarts.py @@ -9,6 +9,7 @@ # 二维坐标系统基础配置适用 scatter,bar,line ECHARTS_BASE_GRID_OPTIONS = { + 'animation': False, 'legend': { 'data': [] }, @@ -70,7 +71,8 @@ var obj = {top: 10}; obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30; return obj; - } + }, + 'extraCssText': 'box-shadow: 0 0 0 rgba(0, 0, 0, 0);' """) }, 'axisPointer': { @@ -618,7 +620,8 @@ def candlestick_echarts(data_frame: pd.DataFrame, time_field: str = 'time', open df[low_field] = df[low_field].fillna(df[close_field]) df['uplimit'] = df[close_field] > df[close_field].shift(1) * 1.098 df['dnlimit'] = df[close_field] < df[close_field].shift(1) * 0.902 - bars = [{'value': [row[open_field], row[close_field], row[low_field], row[high_field]], + df['pct_change']=df[close_field].pct_change() + bars = [{'value': [row[open_field], row[close_field], row[low_field], row[high_field],row['pct_change']], 'itemStyle': ({"borderWidth": 4} if (row['uplimit'] or row['dnlimit']) else {})} for row in df.to_dict(orient="records")] df[volume_field] = df[volume_field].fillna(0) @@ -631,20 +634,21 @@ def candlestick_echarts(data_frame: pd.DataFrame, time_field: str = 'time', open options = { 'animation': False, - 'title': {'text': title}, - 'legend': {'top': 10, 'left': 'center', 'data': [title]}, + 'title': {'text': title,'padding':[5,0]}, + 'legend': {'top': 0,'padding':[5,0], 'left': 'right', 'data': [title]}, 'tooltip': { - 'trigger': 'axis', 'axisPointer': {'type': 'cross'}, - 'borderWidth': 1, - 'borderColor': '#ccc', + 'trigger': 'axis', 'axisPointer': {'type': 'line','snap':True}, + 'borderWidth': 0, + 'borderColor': 'transparent', 'color': "black", - 'backgroundColor': "rgba(255,255,255,0.8)", - 'padding': 10, + 'backgroundColor': "rgba(0,0,0,0)", + 'alwaysShowContent': False, + 'padding': 0, 'formatter': Js(""" function(params){ var dt = params[0]['axisValue']; var labels = []; - labels.push('时间: ' + dt + '
'); + labels.push('时间: ' + dt + ' '); params.sort(function(a, b) { if (a.seriesName < b.seriesName ) {return -1;} else if (a.seriesName > b.seriesName ) {return 1;} @@ -652,42 +656,51 @@ def candlestick_echarts(data_frame: pd.DataFrame, time_field: str = 'time', open }); for (let i = 0; i < params.length; i++) { const param = params[i]; - var label=[""+param['seriesName']+"("+param['seriesType']+"): "]; - var dimensionNames=param["dimensionNames"]; - if (typeof(param['value'])=='object' && dimensionNames.length==param['data'].length){ - label.push("
"); - for (let j = 1; j 100000){ - label.push(""+dimensionNames[j]+': '+value.toFixed(0)+"
"); - }else{ - label.push(""+dimensionNames[j]+': '+value.toFixed(2)+"
"); - } - }else{ - label.push("
"+dimensionNames[j]+': '+value+"
"); + const label = []; + const dimensionNames = param["dimensionNames"]; + if (typeof (param['value']) == 'object' && dimensionNames.length == param['data'].length) { + for (let j = 1; j < dimensionNames.length; j++) { + const value = param['data'][j]; + if (typeof (value) == 'number') { + if (value % 1 == 0 || value > 100000) { + label.push("" + dimensionNames[j] + ': ' + (value / 10000).toFixed(0) + "万 "); + } else { + label.push("" + dimensionNames[j] + ': ' + (value / 10000).toFixed(2) + "万 "); } + } else { + label.push("
" + dimensionNames[j] + ': ' + value + "
 "); + } } - }else if(param['seriesType']=="candlestick"){ - label.push("
"); - label.push("open: "+param['value'][1].toFixed(2)+"
"); - label.push("close: "+param['value'][2].toFixed(2)+"
"); - label.push("high: "+param['value'][4].toFixed(2)+"
"); - label.push("low: "+param['value'][3].toFixed(2)+"
"); - label.push("change: "+((param['value'][2]/param['value'][1]-1)*100).toFixed(2)+"
"); - label.push("range: "+((param['value'][4]/param['value'][3]-1)*100).toFixed(2)+"
"); - }else if(typeof(param['value'])=='number'){ - if (param['value']%1==0){ - label.push(""+param['value'].toFixed(0)+"
"); - }else{ - label.push(""+param['value'].toFixed(2)+"
"); + } else if (param['seriesType'] == "candlestick") { + label.push("开: " + param['value'][1].toFixed(2) + " "); + label.push("高: " + param['value'][4].toFixed(2) + " "); + label.push("低: " + param['value'][3].toFixed(2) + " "); + label.push("收: " + param['value'][2].toFixed(2) + " "); + const increase = (param['value'][5] * 100); + const colors=['#254000','#3f6600','#5b8c00','#7cb305','#a0d911','#f5222d','#cf1322','#a8071a','#820014',"#5c0011"]; + var color_index = Math.round((Math.min(Math.max(-10,increase),10)+10)/2-1); + label.push("涨幅: " + increase.toFixed(2) + " "); + label.push("振幅: " + ((param['value'][4] / param['value'][3] - 1) * 100).toFixed(2) + " "); + } else if (typeof (param['value']) == 'number') { + if (param['value'] > 10000) { + if (param['value'] % 1 == 0) { + label.push("" + param["seriesName"] + ":" + (param['value'] / 10000).toFixed(0) + "万 "); + } else { + label.push("" + param["seriesName"] + ":" + (param['value'] / 10000).toFixed(2) + "万 "); + } + } else { + if (param['value'] % 1 == 0) { + label.push("" + param["seriesName"] + ":" + param['value'].toFixed(0) + " "); + } else { + label.push("" + param["seriesName"] + ":" + param['value'].toFixed(2) + " "); + } } - }else if(param['value']){ - label.push("
"+value+"
"); - }else{ - label.push("
"); + } else if (param['value']) { + label.push("
" + param['value'] + "
 "); + } else { + label.push(" "); } - var cardStr= label.join(''); + const cardStr = label.join(''); labels.push(cardStr); } return labels.join(''); @@ -695,26 +708,25 @@ def candlestick_echarts(data_frame: pd.DataFrame, time_field: str = 'time', open 'textStyle': {'color': '#000'}, 'position': Js(""" function (pos, params, el, elRect, size){ - var obj = {top: 10}; - obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30; - return obj; + return {top: 30, left: 0}; } - """) + """), + 'extraCssText': 'box-shadow: 0 0 0 rgba(0, 0, 0, 0);' }, 'axisPointer': { 'link': {'xAxisIndex': 'all'}, 'label': {'backgroundColor': '#777'} }, 'toolbox': { - 'show': True, + 'show': False, 'feature': { 'restore': {}, 'saveAsImage': {} } }, 'grid': [ - {'left': left_padding, 'right': right_padding, 'height': '70%'}, - {'left': left_padding, 'right': right_padding, 'top': '71%', 'height': '18%'} + {'left': left_padding, 'right': right_padding, 'height': '75%'}, + {'left': left_padding, 'right': right_padding, 'top': '76%', 'height': '13%'} ], 'xAxis': [ { @@ -835,7 +847,7 @@ def candlestick_echarts(data_frame: pd.DataFrame, time_field: str = 'time', open 'type': 'line', 'data': df[name].tolist(), 'smooth': True, - 'showSymbol': False, + 'symbol': 'none', 'lineStyle': {'opacity': 0.5} } options['series'].append(series_ma) @@ -877,6 +889,7 @@ def heatmap_echarts(data_frame: pd.DataFrame, x_field: str = None, y_field: str df[label_field + '_label'] = data_frame[label_field] df[color_field + '_color'] = data_frame[color_field] options = { + 'animation': False, 'title': {'text': title}, 'tooltip': { 'position': 'top', @@ -958,6 +971,7 @@ def radar_echarts(data_frame: pd.DataFrame, name_field: str = None, indicator_fi :return: """ options = { + 'animation': False, 'title': { 'text': title }, @@ -1034,6 +1048,7 @@ def calendar_heatmap_echarts(data_frame: pd.DataFrame, date_field: str = None, v date_end = pd.to_datetime(df[date_field].max()).strftime("%Y-%m-%d") df[date_field] = pd.to_datetime(df[date_field]).dt.strftime("%Y-%m-%d") options = { + 'animation': False, 'title': { 'text': title },