Android highcharts 显示单项条形图
Android highcharts display single entry bar chart
我正在 android 中处理 highcharts
。我正在尝试显示带有单个条目的 barchart。下面是代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HIChartView chartView = findViewById(R.id.hc);
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("bar");
options.setChart(chart);
HITitle title = new HITitle();
title.setText("Historic World Population by Region");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Source: <a href=\\"https://en.wikipedia.org/wiki/World_population\\">Wikipedia.org</a>Source: <a href=\\"https://en.wikipedia.org/wiki/World_population\\">Wikipedia.org</a>");
options.setSubtitle(subtitle);
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "Africa", "America", "Asia", "Europe", "Oceania"};
xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});
HIYAxis yaxis = new HIYAxis();
yaxis.setMin(0);
yaxis.setTitle(new HITitle());
yaxis.getTitle().setText("Population (millions)");
yaxis.getTitle().setAlign("high");
yaxis.setLabels(new HILabels());
yaxis.getLabels().setOverflow("justify");
options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});
HITooltip tooltip = new HITooltip();
tooltip.setValueSuffix(" millions");
options.setTooltip(tooltip);
HILegend legend = new HILegend();
legend.setLayout("vertical");
legend.setAlign("right");
legend.setVerticalAlign("top");
legend.setX(-40);
legend.setY(80);
legend.setFloating(true);
legend.setBorderWidth(1);
legend.setBackgroundColor(HIColor.initWithHexValue("FFFFFF"));
legend.setShadow(true);
options.setLegend(legend);
HICredits credits = new HICredits();
credits.setEnabled(false);
options.setCredits(credits);
HIBar bar1 = new HIBar();
bar1.setName("Year 1800");
Number[] bar1Data = new Number[] {107, 31, 635, 203, 2 };
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
HIBar bar2 = new HIBar();
bar2.setName("Year 1900");
Number[] bar2Data = new Number[] { 133, 156, 947, 408, 6 };
bar2.setData(new ArrayList<>(Arrays.asList(bar2Data)));
HIBar bar3 = new HIBar();
bar3.setName("Year 2012");
Number[] bar3Data = new Number[] { 1052, 954, 4250, 740, 38 };
bar3.setData(new ArrayList<>(Arrays.asList(bar3Data)));
options.setSeries(new ArrayList<>(Arrays.asList(bar1, bar2,bar3)));
chartView.setOptions(options);
}
}
输出
预期输出
我想要每个月的单个条目,如下所示。
我试过的
我试过下面的代码
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "Mar-2021", "Apr-2021","May-2021"};
xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});
HIBar bar1 = new HIBar();
bar1.setName("March");
Number[] bar1Data = new Number[]{10};
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
HIBar bar2 = new HIBar();
bar2.setName("April");
Number[] bar2Data = new Number[] {15};
bar2.setData(new ArrayList<>(Arrays.asList(bar2Data)));
HIBar bar3 = new HIBar();
bar3.setName("May");
Number[] bar3Data = new Number[] {20};
bar3.setData(new ArrayList<>(Arrays.asList(bar3Data)));
输出
输出结果与我预期的不同,因为它显示了针对单个月份的所有 3 个值
更新 1
我找到了一个网络解决方案,但不幸的是,我无法在我的 android 代码中做同样的事情。这是 fiddle
您需要增加数据量,而不是系列量。所以不是多个系列:
HIBar bar1 = new HIBar();
Number[] bar1Data = new Number[]{10};
...
HIBar bar2 = new HIBar();
Number[] bar2Data = new Number[] {15};
...
HIBar bar3 = new HIBar();
Number[] bar3Data = new Number[] {20};
...
使用一个包含多个点的系列:
HIBar bar1 = new HIBar();
Number[] bar1Data = new Number[] {107, 31, 635, 203, 2 };
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
我正在 android 中处理 highcharts
。我正在尝试显示带有单个条目的 barchart。下面是代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HIChartView chartView = findViewById(R.id.hc);
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("bar");
options.setChart(chart);
HITitle title = new HITitle();
title.setText("Historic World Population by Region");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Source: <a href=\\"https://en.wikipedia.org/wiki/World_population\\">Wikipedia.org</a>Source: <a href=\\"https://en.wikipedia.org/wiki/World_population\\">Wikipedia.org</a>");
options.setSubtitle(subtitle);
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "Africa", "America", "Asia", "Europe", "Oceania"};
xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});
HIYAxis yaxis = new HIYAxis();
yaxis.setMin(0);
yaxis.setTitle(new HITitle());
yaxis.getTitle().setText("Population (millions)");
yaxis.getTitle().setAlign("high");
yaxis.setLabels(new HILabels());
yaxis.getLabels().setOverflow("justify");
options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});
HITooltip tooltip = new HITooltip();
tooltip.setValueSuffix(" millions");
options.setTooltip(tooltip);
HILegend legend = new HILegend();
legend.setLayout("vertical");
legend.setAlign("right");
legend.setVerticalAlign("top");
legend.setX(-40);
legend.setY(80);
legend.setFloating(true);
legend.setBorderWidth(1);
legend.setBackgroundColor(HIColor.initWithHexValue("FFFFFF"));
legend.setShadow(true);
options.setLegend(legend);
HICredits credits = new HICredits();
credits.setEnabled(false);
options.setCredits(credits);
HIBar bar1 = new HIBar();
bar1.setName("Year 1800");
Number[] bar1Data = new Number[] {107, 31, 635, 203, 2 };
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
HIBar bar2 = new HIBar();
bar2.setName("Year 1900");
Number[] bar2Data = new Number[] { 133, 156, 947, 408, 6 };
bar2.setData(new ArrayList<>(Arrays.asList(bar2Data)));
HIBar bar3 = new HIBar();
bar3.setName("Year 2012");
Number[] bar3Data = new Number[] { 1052, 954, 4250, 740, 38 };
bar3.setData(new ArrayList<>(Arrays.asList(bar3Data)));
options.setSeries(new ArrayList<>(Arrays.asList(bar1, bar2,bar3)));
chartView.setOptions(options);
}
}
输出
预期输出
我想要每个月的单个条目,如下所示。
我试过的
我试过下面的代码
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "Mar-2021", "Apr-2021","May-2021"};
xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});
HIBar bar1 = new HIBar();
bar1.setName("March");
Number[] bar1Data = new Number[]{10};
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
HIBar bar2 = new HIBar();
bar2.setName("April");
Number[] bar2Data = new Number[] {15};
bar2.setData(new ArrayList<>(Arrays.asList(bar2Data)));
HIBar bar3 = new HIBar();
bar3.setName("May");
Number[] bar3Data = new Number[] {20};
bar3.setData(new ArrayList<>(Arrays.asList(bar3Data)));
输出
输出结果与我预期的不同,因为它显示了针对单个月份的所有 3 个值
更新 1
我找到了一个网络解决方案,但不幸的是,我无法在我的 android 代码中做同样的事情。这是 fiddle
您需要增加数据量,而不是系列量。所以不是多个系列:
HIBar bar1 = new HIBar();
Number[] bar1Data = new Number[]{10};
...
HIBar bar2 = new HIBar();
Number[] bar2Data = new Number[] {15};
...
HIBar bar3 = new HIBar();
Number[] bar3Data = new Number[] {20};
...
使用一个包含多个点的系列:
HIBar bar1 = new HIBar();
Number[] bar1Data = new Number[] {107, 31, 635, 203, 2 };
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));