- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 OHLC 图表上叠加移动平均线,但它似乎不起作用。
我提供了代码的两个关键功能:
private static OHLCDataset createPriceDataset(String filename)
{
OHLCSeries s1 = new OHLCSeries(filename);
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse( st.nextToken() );
double open = Double.parseDouble( st.nextToken() );
double high = Double.parseDouble( st.nextToken() );
double low = Double.parseDouble( st.nextToken() );
double close = Double.parseDouble( st.nextToken() );
double volume = Double.parseDouble( st.nextToken() );
//double adjClose = Double.parseDouble( st.nextToken() );
s1.add(new Day(date), open, high, low, close);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
private static JFreeChart createCombinedChart()
{
OHLCDataset data1 = createPriceDataset(filename);
XYItemRenderer renderer1 = new HighLowRenderer();
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
renderer1.setSeriesPaint(0, Color.blue);
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
rangeAxis.setAutoRange(true);
rangeAxis.setAutoRangeIncludesZero(false);
XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
plot1.setBackgroundPaint(Color.lightGray);
plot1.setDomainGridlinePaint(Color.white);
plot1.setRangeGridlinePaint(Color.white);
plot1.setRangePannable(true);
//Overlay the Long-Term Trend Indicator
XYDataset dataset3 = MovingAverage.createMovingAverage(data1, "LT", 49.0, 49.0);
plot1.setDataset(1, dataset3);
plot1.setRenderer(1, new StandardXYItemRenderer());
//add a second dataset (volume) and renderer
IntervalXYDataset data2 = createVolumeDataset(filename);
XYBarRenderer renderer2 = new XYBarRenderer();
renderer2.setDrawBarOutline(false);
renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
renderer2.setSeriesPaint(0, Color.red);
XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2);
plot2.setBackgroundPaint(Color.lightGray);
plot2.setDomainGridlinePaint(Color.white);
plot2.setRangeGridlinePaint(Color.white);
CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
cplot.add(plot1, 3);
cplot.add(plot2, 2);
cplot.setGap(8.0);
cplot.setDomainGridlinePaint(Color.white);
cplot.setDomainGridlinesVisible(true);
cplot.setDomainPannable(true);
//return the new combined chart
JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)", JFreeChart.DEFAULT_TITLE_FONT, cplot, false);
ChartUtilities.applyCurrentTheme(chart);
renderer2.setShadowVisible(false);
renderer2.setBarPainter(new StandardXYBarPainter());
return chart;
}
有什么想法吗?
谢谢。
最佳答案
我添加了以下代码,它似乎可以工作。如果有人不同意这个解决方案,请告诉我。开始了:
public class PriceVolumeChart2 extends ApplicationFrame{
final static String filename = "A.txt";
*static TimeSeries t1 = new TimeSeries("49-day moving average");*
/**
* Default constructor
*/
public PriceVolumeChart2(String title)
{
super(title);
JPanel panel = createDemoPanel();
panel.setPreferredSize(new Dimension(500, 270));
setContentPane(panel);
}
//create price dataset
//hard-coded here
private static OHLCDataset createPriceDataset(String filename)
{
//the following data is taken from http://finance.yahoo.com/
//for demo purposes...
OHLCSeries s1 = new OHLCSeries(filename);
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse( st.nextToken() );
double open = Double.parseDouble( st.nextToken() );
double high = Double.parseDouble( st.nextToken() );
double low = Double.parseDouble( st.nextToken() );
double close = Double.parseDouble( st.nextToken() );
double volume = Double.parseDouble( st.nextToken() );
//double adjClose = Double.parseDouble( st.nextToken() );
s1.add(new Day(date), open, high, low, close);
*t1.add(new Day(date), close);*
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
//create volume dataset
private static IntervalXYDataset createVolumeDataset(String filename)
{
//create dataset 2...
TimeSeries s1 = new TimeSeries("Volume");
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse( st.nextToken() );
st.nextToken();
st.nextToken();
st.nextToken();
st.nextToken();
double volume = Double.parseDouble( st.nextToken() );
//double adjClose = Double.parseDouble( st.nextToken() );
s1.add(new Day(date), volume);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
return new TimeSeriesCollection(s1);
}
private static JFreeChart createCombinedChart()
{
OHLCDataset data1 = createPriceDataset(filename);
XYItemRenderer renderer1 = new HighLowRenderer();
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
renderer1.setSeriesPaint(0, Color.blue);
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
rangeAxis.setAutoRange(true);
rangeAxis.setAutoRangeIncludesZero(false);
XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
plot1.setBackgroundPaint(Color.lightGray);
plot1.setDomainGridlinePaint(Color.white);
plot1.setRangeGridlinePaint(Color.white);
plot1.setRangePannable(true);
//Overlay the Long-Term Trend Indicator
*TimeSeries dataset3 = MovingAverage.createMovingAverage(t1, "LT", 49, 49);
TimeSeriesCollection collection = new TimeSeriesCollection();
collection.addSeries(dataset3);
plot1.setDataset(1, collection);*
plot1.setRenderer(1, new StandardXYItemRenderer());
//add a second dataset (volume) and renderer
IntervalXYDataset data2 = createVolumeDataset(filename);
XYBarRenderer renderer2 = new XYBarRenderer();
renderer2.setDrawBarOutline(false);
renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
renderer2.setSeriesPaint(0, Color.red);
XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2);
plot2.setBackgroundPaint(Color.lightGray);
plot2.setDomainGridlinePaint(Color.white);
plot2.setRangeGridlinePaint(Color.white);
CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
cplot.add(plot1, 3);
cplot.add(plot2, 2);
cplot.setGap(8.0);
cplot.setDomainGridlinePaint(Color.white);
cplot.setDomainGridlinesVisible(true);
cplot.setDomainPannable(true);
//return the new combined chart
JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)",
JFreeChart.DEFAULT_TITLE_FONT, cplot, false);
ChartUtilities.applyCurrentTheme(chart);
renderer2.setShadowVisible(false);
renderer2.setBarPainter(new StandardXYBarPainter());
return chart;
}
//create a panel
public static JPanel createDemoPanel()
{
JFreeChart chart = createCombinedChart();
return new ChartPanel(chart);
}
public static void main(String[] args) {
// TODO code application logic here
PriceVolumeChart2 demo = new PriceVolumeChart2(
"JFreeChart: CombinedXYPlotDemo1.java (base)");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
//Download data from web
}
示例数据:
20110103,41.56,42.14,41.41,41.88,357230020110104,41.99,42.1,41.18,41.49,358880020110105,41.28,41.73,41,41.4,323270020110106,41.37,41.84,41.21,41.48,336140020110107,41.52,41.8,41.04,41.62,272590020110110,41.41,42.72,41.3,42.22,314580020110111,42.52,43.31,42.38,42.94,331540020110112,43.2,43.41,42.96,43.13,246310020110113,42.88,43.23,42.87,42.97,167640020110114,42.79,43.37,42.76,43.26,221560020110117,43.26,43.26,43.26,43.26,020110118,43.33,44.45,43.32,44.35,298230020110119,44.16,44.29,42.27,42.43,453720020110120,41.95,42.58,41.46,42.29,487470020110121,42.5,43.26,42.03,42.11,300450020110124,42.18,42.79,42.07,42.77,206740020110125,42.77,43.52,42.28,42.69,313270020110126,42.82,42.97,42.04,42.57,392730020110127,42.77,43.09,42.37,42.45,318960020110128,42.5,42.52,40.88,40.98,362980020110131,41.21,41.83,40.89,41.83,369090020110201,42.07,42.7,41.93,42.05,338820020110202,41.75,41.91,40.82,41.23,397070020110203,40.93,41.18,40.23,40.99,352270020110204,41.19,43.13,40.94,42.99,519770020110207,43.45,44.66,43.37,44.44,456980020110208,44.65,44.71,43.83,44.17,373450020110209,44.01,44.17,43.16,43.43,377930020110210,43.22,44.01,42.57,44,327570020110211,43.77,45.15,43.65,45.02,443600020110214,45,45.42,44.72,44.79,348440020110215,44.79,45,42.64,42.65,732800020110216,42.91,43.6,42.7,43.57,415980020110217,43.38,44,43.02,43.92,262180020110218,43.78,44.29,43.65,43.92,439020020110221,43.92,43.92,43.92,43.92,020110222,43.21,43.75,42.39,42.49,414360020110223,42.59,42.75,39.94,40.45,707420020110224,40.34,41.55,40.18,41.15,445570020110225,41.16,42.41,41.13,42.36,429750020110228,42.43,42.65,41.36,42.08,307020020110301,41.98,42.49,40.65,40.68,4091300
新代码在星号内。
如果您认为有更优雅的解决方案,请告诉我。
关于JFreeChart : How to draw the moving average over a OHLC chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330370/
我有一个通过 Google Charts Org Chart API 呈现的图表。当用户单击节点时,如何获取有关所选节点的数据。我理解到我进行“getSelection”调用以将选择信息输出到 Jav
我使用Chart.js显示雷达图。我的问题是某些标签很长: 图表无法显示或显得很小。 那么,有没有办法换行或为标签分配最大宽度? 感谢您的帮助! 最佳答案 对于Chart.js 2.0+,您可以使用数
我想问一下使用Chart.js http://www.chartjs.org/ 可以吗?获得组合条形图和折线图? 感谢您的任何建议。 最佳答案 下面的答案与chart.js 1.x有关。 Chart.
我在addData和removeData函数中添加了chart.resize(),但它总是增加高度并且永远不会减少! 当用户单击菜单项时,我正在更新图表。有些项目有超过 100 个数据,有些项目只有
horizontal bar chart var barOptions_stacked1 = { tooltips: { e
我们将 Chart.js 与 clojurescript 和 Reagent 一起使用。现在我知道 Chart.js 有一个 Chart.update() 方法来用新数据更新图表。所以问题是,如何设置
我想显示一个包含多个数据集的条形图,如下所示,。。我可以获取如下所示的数据。。而是如何转换这个JSON数组,如下所示。我需要显示过去7天每个班次的出勤百分比。。感谢你在这方面的帮助。谢谢!。=编辑=
I want to display a bar chart with multiple datasets as shown below,我想显示一个包含多个数据集的条形图,如下所示, I'
假设我有一个 Charting.Chart : 我想导出到 Excel.Workbook.Worksheet以便我稍后可以“播放”数据(例如在 Excel 图表上拖放更多数据等): 请不要介意第二张图
离子性: ionic(Ionic CLI):4.10.3(/Users/faiz/.nvm/versions/node/v8.9.4/lib/node_modules/ionic) 离子框架:离子角3
我正在使用Chart.js来可视化我的数据,如下图所示。。。我想在图表中间加一张照片。。。如何在甜甜圈图表的中间添加照片?下面是我的javascript代码:
我正在使用 Kendo 饼图,我想将其配置为在没有数据可显示时显示一条消息。我正在使用以下 js 函数配置饼图 - 变量“JsPieChartDataSet”是一组 JSON 格式的数据。 是否有可以
是否可以在图表和x轴之间获得更多空间? 是否可以在图表的右侧和 Canvas 区域的末端之间留出更多的空间?我想在图表旁边的 Canvas 中添加更多元素,但是这是不可能的,因为图表占用了整个 Can
我正在使用chart.js,在图表的某些点的多日条目之间缺少一些数据。我已将这些值分配为null,但希望图表在缺失点之间绘制一条连接线。这是我所拥有的: 有没有办法将chart.js中的点连接起来?也
我正在生成一些谷歌图表,但我被困在这里。 Google 允许您将列堆叠起来。但它要么受到限制,要么我无法将其配置为工作。取自谷歌,这里有一个例子,显示了两个国家每年生产的咖啡杯数: 假设我有另一个相同
使用 Google Charts 条形图,是否可以更改一个条形的颜色。例如,我想将 2006 年的数据设为红色(其他条形为蓝色)。 function drawVisualization() {
我目前正在尝试重新设计设计布局。我通过尝试和错误得到了大部分。我唯一不明白的部分是: 1.) 左侧和底部的黑色轴。所以只有那些是黑色的2.) 里面的浅灰色轴。如何在颜色上应用不透明度或使用我自己的颜色
我使用以下命令创建了一个名为“abc”的 Helm 图表helm create abc现在,当我安装此图表时,所有创建的kuberenets资源将具有一个包含“abc”的名称。 现在,我必须将图表“a
我正在构建一个折线图,除了 ChartScrollbar 中的标签之外,一切正常,因为类别名称有点长,cahrtScrollbar 中的标签相互堆叠使其无法读取。我想知道是否可以隐藏标签、仅显示一些标
我正在使用 Chart.js 2.5。我的情况是,我有一个包含 40 个数据的数据集,但我只想在折线图中显示 7 个数据,但仍然可以水平向左/向右移动以发现其余数据。 我尝试了ticks.maxTic
我是一名优秀的程序员,十分优秀!