forked from p85126437/datagear
完善图表渲染逻辑,将name,updateInterval改为写入Chart.propertyValues中,便于统一概念和逻辑
This commit is contained in:
parent
e870ed3a72
commit
d45be07544
|
@ -23,24 +23,47 @@ import org.datagear.analysis.RenderException;
|
|||
*/
|
||||
public class ChartWidget<T extends RenderContext> extends AbstractIdentifiable
|
||||
{
|
||||
/** 图表名称属性名 */
|
||||
public static final String CHART_PROPERTY_VALUE_NAME = "name";
|
||||
|
||||
/** 图表更新间隔属性名 */
|
||||
public static final String CHART_PROPERTY_VALUE_UPDATE_INTERVAL = "updateInterval";
|
||||
|
||||
/** 图表名称 */
|
||||
private String name = "";
|
||||
|
||||
private ChartPlugin<T> chartPlugin;
|
||||
|
||||
private ChartPropertyValues chartPropertyValues;
|
||||
private ChartPropertyValues chartPropertyValues = new ChartPropertyValues();
|
||||
|
||||
private DataSetFactory[] dataSetFactories;
|
||||
private DataSetFactory[] dataSetFactories = new DataSetFactory[0];
|
||||
|
||||
/** 图表更新间隔毫秒数 */
|
||||
private int updateInterval = -1;
|
||||
|
||||
public ChartWidget()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public ChartWidget(String id, ChartPlugin<T> chartPlugin, DataSetFactory... dataSetFactories)
|
||||
public ChartWidget(String id, String name, ChartPlugin<T> chartPlugin, DataSetFactory... dataSetFactories)
|
||||
{
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.chartPlugin = chartPlugin;
|
||||
this.dataSetFactories = dataSetFactories;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ChartPlugin<T> getChartPlugin()
|
||||
{
|
||||
return chartPlugin;
|
||||
|
@ -71,6 +94,21 @@ public class ChartWidget<T extends RenderContext> extends AbstractIdentifiable
|
|||
this.dataSetFactories = dataSetFactories;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图表更新间隔毫秒数。
|
||||
*
|
||||
* @return {@code <0}:不间隔更新;0 :实时更新;{@code >0}:间隔更新毫秒数
|
||||
*/
|
||||
public int getUpdateInterval()
|
||||
{
|
||||
return updateInterval;
|
||||
}
|
||||
|
||||
public void setUpdateInterval(int updateInterval)
|
||||
{
|
||||
this.updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染{@linkplain Chart}。
|
||||
*
|
||||
|
@ -80,6 +118,13 @@ public class ChartWidget<T extends RenderContext> extends AbstractIdentifiable
|
|||
*/
|
||||
public Chart render(T renderContext) throws RenderException
|
||||
{
|
||||
return this.chartPlugin.renderChart(renderContext, this.chartPropertyValues, this.dataSetFactories);
|
||||
ChartPropertyValues propertyValues = new ChartPropertyValues();
|
||||
propertyValues.put(CHART_PROPERTY_VALUE_NAME, this.name);
|
||||
propertyValues.put(CHART_PROPERTY_VALUE_UPDATE_INTERVAL, this.updateInterval);
|
||||
|
||||
if (this.chartPropertyValues != null)
|
||||
propertyValues.putAll(this.chartPropertyValues);
|
||||
|
||||
return this.chartPlugin.renderChart(renderContext, propertyValues, this.dataSetFactories);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,14 +240,9 @@ public class HtmlChartPlugin<T extends HtmlRenderContext> extends AbstractChartP
|
|||
chartVarName = HtmlRenderAttributes.generateChartVarName(nextSequence);
|
||||
}
|
||||
|
||||
Integer updateInterval = HtmlRenderAttributes.getChartUpdateInterval(renderContext);
|
||||
|
||||
HtmlChart chart = new HtmlChart(IDUtil.uuid(), renderContext, this, chartPropertyValues, dataSetFactories,
|
||||
chartElementId, chartVarName);
|
||||
|
||||
if (updateInterval != null)
|
||||
chart.setUpdateInterval(updateInterval);
|
||||
|
||||
try
|
||||
{
|
||||
writeChartScript(renderContext, chart);
|
||||
|
|
|
@ -20,17 +20,14 @@ import org.datagear.analysis.support.ChartWidget;
|
|||
*/
|
||||
public class HtmlChartWidget<T extends HtmlRenderContext> extends ChartWidget<T>
|
||||
{
|
||||
/** 更新间隔毫秒数 */
|
||||
private int updateInterval = -1;
|
||||
|
||||
public HtmlChartWidget()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public HtmlChartWidget(String id, HtmlChartPlugin<T> chartPlugin, DataSetFactory... dataSetFactories)
|
||||
public HtmlChartWidget(String id, String name, HtmlChartPlugin<T> chartPlugin, DataSetFactory... dataSetFactories)
|
||||
{
|
||||
super(id, chartPlugin, dataSetFactories);
|
||||
super(id, name, chartPlugin, dataSetFactories);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -51,25 +48,8 @@ public class HtmlChartWidget<T extends HtmlRenderContext> extends ChartWidget<T>
|
|||
@Override
|
||||
public HtmlChart render(T renderContext) throws RenderException
|
||||
{
|
||||
HtmlRenderAttributes.setChartUpdateInterval(renderContext, this.updateInterval);
|
||||
HtmlChart chart = (HtmlChart) super.render(renderContext);
|
||||
HtmlRenderAttributes.removeChartUpdateInterval(renderContext);
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图表更新间隔毫秒数。
|
||||
*
|
||||
* @return {@code <0}:不间隔更新;0 :实时更新;{@code >0}:间隔更新毫秒数
|
||||
*/
|
||||
public int getUpdateInterval()
|
||||
{
|
||||
return updateInterval;
|
||||
}
|
||||
|
||||
public void setUpdateInterval(int updateInterval)
|
||||
{
|
||||
this.updateInterval = updateInterval;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,8 +45,6 @@ public class HtmlRenderAttributes
|
|||
|
||||
public static final String CHART_SCRIPT_NOT_INVOKE_RENDER = "chartScriptNotInvokeRender";
|
||||
|
||||
public static final String CHART_UPDATE_INTERVAL = "chartUpdateInterval";
|
||||
|
||||
/**
|
||||
* 获取{@linkplain RenderStyle},没有则返回{@code null}。
|
||||
*
|
||||
|
@ -385,39 +383,6 @@ public class HtmlRenderAttributes
|
|||
return renderContext.removeAttribute(CHART_SCRIPT_NOT_INVOKE_RENDER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图表更新间隔,没有则返回{@code null}。
|
||||
*
|
||||
* @param renderContext
|
||||
* @return
|
||||
*/
|
||||
public static Integer getChartUpdateInterval(RenderContext renderContext)
|
||||
{
|
||||
return renderContext.getAttribute(CHART_UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图表更新间隔。
|
||||
*
|
||||
* @param renderContext
|
||||
* @param chartUpdateInterval
|
||||
*/
|
||||
public static void setChartUpdateInterval(RenderContext renderContext, int chartUpdateInterval)
|
||||
{
|
||||
renderContext.setAttribute(CHART_UPDATE_INTERVAL, chartUpdateInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除图表更新间隔。
|
||||
*
|
||||
* @param renderContext
|
||||
* @return
|
||||
*/
|
||||
public static Integer removeChartUpdateInterval(RenderContext renderContext)
|
||||
{
|
||||
return renderContext.removeAttribute(CHART_UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一个序列。
|
||||
* <p>
|
||||
|
|
|
@ -102,6 +102,7 @@ public abstract class HtmlTplDashboardWidgetRenderer<T extends HtmlRenderContext
|
|||
|
||||
private HtmlChartWidget<HtmlRenderContext> htmlChartWidgetForNotFound = new HtmlChartWidget<HtmlRenderContext>(
|
||||
StringUtil.firstLowerCase(Global.PRODUCT_NAME_EN) + "HtmlChartWidgetForNotFound",
|
||||
"HtmlChartWidgetForNotFound",
|
||||
new ValueHtmlChartPlugin<HtmlRenderContext>(
|
||||
StringUtil.firstLowerCase(Global.PRODUCT_NAME_EN) + "HtmlChartPluginForNotFound",
|
||||
RENDER_ATTR_NAME_FOR_NOT_FOUND_SCRIPT));
|
||||
|
|
|
@ -34,6 +34,7 @@ public class HtmlTplDashboardWidgetFmkRendererTest
|
|||
HtmlChartPlugin<HtmlRenderContext> chartPlugin = HtmlChartPluginTest.createHtmlChartPlugin();
|
||||
|
||||
HtmlChartWidget<HtmlRenderContext> htmlChartWidget = new HtmlChartWidget<HtmlRenderContext>("chart-widget-01",
|
||||
"chart-widget-01",
|
||||
chartPlugin, (DataSetFactory[]) null);
|
||||
|
||||
DashboardWidgetResManager resManager = new DashboardWidgetResManager(
|
||||
|
|
|
@ -47,6 +47,7 @@ public class HtmlTplDashboardWidgetHtmlRendererTest
|
|||
HtmlChartPlugin<HtmlRenderContext> chartPlugin = HtmlChartPluginTest.createHtmlChartPlugin();
|
||||
|
||||
HtmlChartWidget<HtmlRenderContext> htmlChartWidget = new HtmlChartWidget<HtmlRenderContext>("chart-widget-01",
|
||||
"chart-widget-01",
|
||||
chartPlugin, (DataSetFactory[]) null);
|
||||
|
||||
DashboardWidgetResManager resManager = new DashboardWidgetResManager(
|
||||
|
|
|
@ -30,9 +30,6 @@ public class HtmlChartWidgetEntity extends HtmlChartWidget<HtmlRenderContext>
|
|||
/** 授权资源类型 */
|
||||
public static final String AUTHORIZATION_RESOURCE_TYPE = "Chart";
|
||||
|
||||
/** 名称 */
|
||||
private String name;
|
||||
|
||||
/** 创建用户 */
|
||||
private User createUser;
|
||||
|
||||
|
@ -45,13 +42,15 @@ public class HtmlChartWidgetEntity extends HtmlChartWidget<HtmlRenderContext>
|
|||
public HtmlChartWidgetEntity()
|
||||
{
|
||||
super();
|
||||
super.setDataSetFactories(new SqlDataSetFactoryEntity[0]);
|
||||
this.createTime = new Date();
|
||||
}
|
||||
|
||||
public HtmlChartWidgetEntity(String id, HtmlChartPlugin<HtmlRenderContext> chartPlugin,
|
||||
SqlDataSetFactory[] dataSetFactories, String name, User createUser)
|
||||
public HtmlChartWidgetEntity(String id, String name, HtmlChartPlugin<HtmlRenderContext> chartPlugin,
|
||||
SqlDataSetFactory[] dataSetFactories, User createUser)
|
||||
{
|
||||
super(id, chartPlugin, dataSetFactories);
|
||||
super(id, name, chartPlugin, dataSetFactories);
|
||||
this.createUser = createUser;
|
||||
this.createTime = new Date();
|
||||
}
|
||||
|
||||
|
@ -90,16 +89,6 @@ public class HtmlChartWidgetEntity extends HtmlChartWidget<HtmlRenderContext>
|
|||
setDataSetFactories(sqlDataSetFactoryEntities);
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getCreateUser()
|
||||
{
|
||||
|
|
|
@ -57,7 +57,7 @@
|
|||
var legendData = chartUtil.dataset.columnValues(dataSet, xcolumnMeta);
|
||||
var datas = chartUtil.dataset.columnNameValues(dataSet, xcolumnMeta, ycolumnMeta);
|
||||
|
||||
var options = { legend : { data : legendData }, series : [ { name : chart.name, data : datas } ] };
|
||||
var options = { legend : { data : legendData }, series : [ { name : chartUtil.propertyValueName(chart), data : datas } ] };
|
||||
this.echarts.chart.setOption(options);
|
||||
};
|
||||
})
|
||||
|
|
|
@ -29,6 +29,35 @@
|
|||
{
|
||||
return this.renderContextAttr(chart, "renderStyle");
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取/设置图表的"name"属性值。
|
||||
*
|
||||
* @param chart
|
||||
* @param value 可选,要设置的属性值
|
||||
*/
|
||||
util.propertyValueName = function(chart, value)
|
||||
{
|
||||
return this.propertyValue(chart, "name", value);
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取/设置图表属性值。
|
||||
*
|
||||
* @param chart
|
||||
* @param name
|
||||
* @param value 可选,要设置的属性值
|
||||
*/
|
||||
util.propertyValue = function(chart, name, value)
|
||||
{
|
||||
if(!chart.propertyValues)
|
||||
chart.propertyValues = {};
|
||||
|
||||
if(value == undefined)
|
||||
return chart.propertyValues[name];
|
||||
else
|
||||
chart.propertyValues[name] = value;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取/设置图表渲染上下文的属性值。
|
||||
|
|
|
@ -130,12 +130,12 @@
|
|||
var chart = charts[i];
|
||||
|
||||
//不需更新
|
||||
if(chart.updateInterval < 0)
|
||||
if(chart.propertyValues.updateInterval < 0)
|
||||
continue;
|
||||
|
||||
var prevUpdateTime = this.updateTime(chart);
|
||||
|
||||
if(prevUpdateTime == null || (prevUpdateTime + chart.updateInterval) <= time)
|
||||
if(prevUpdateTime == null || (prevUpdateTime + chart.propertyValues.updateInterval) <= time)
|
||||
nexts.push(chart);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue