我正在努力android
。我有两个spinners
我试图做一些选择。完成选择后,会调用两个函数,然后显示图表。默认情况下,我已将gone
图表布局的可见性设置为。此外,第二微调器被设置为visibility= gone
并visible
基于第一微调器选择设置。
XML
<LinearLayout
android:id="@+id/golyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/prodLayout"
android:padding="10dp"
android:visibility="gone"
android:orientation="horizontal">
<com.highsoft.highcharts.core.HIChartView
android:id="@+id/golyChart"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_margin="10dp"
android:visibility="visible"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/golmLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/golyLayout"
android:padding="10dp"
android:visibility="gone"
android:orientation="horizontal">
<com.highsoft.highcharts.core.HIChartView
android:id="@+id/golmChart"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_margin="10dp"
android:visibility="visible"
/>
</LinearLayout>
分段
public class SalesFragment extends Fragment {
@BindView(R.id.golyChart)
HIChartView golyChart;
@BindView(R.id.golmChart)
HIChartView golmChart;
@BindView(R.id.golyLayout)
LinearLayout golyChartLayout;
@BindView(R.id.golmLayout)
LinearLayout golmChartLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContext = getActivity();
view = inflater.inflate(R.layout.sales_layout, container, false);
ButterKnife.bind(this, view);
bindListners();
getActivity().setTitle(getString(R.string.title_dashboard));
return view;
}
private void bindListners() {
catAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, catList);
catSpinner.setAdapter(catAdapter);
catSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedCategory = catList.get(position);
//Toast.makeText(getActivity(),selectedCategory,Toast.LENGTH_LONG).show();
if (selectedCategory.equals("Product Wise"))
{
prodLayout.setVisibility(View.VISIBLE);
prodSpinner.setSelection(-1);
if (golyChartLayout.getVisibility()==View.VISIBLE && golmChartLayout.getVisibility()==View.VISIBLE)
{
golyChartLayout.setVisibility(View.GONE);
golmChartLayout.setVisibility(View.GONE);
}
}
else if(selectedCategory.equals("Group Wise"))
{
prodLayout.setVisibility(View.GONE);
prodSpinner.setSelection(-1);
golyChartLayout.setVisibility(View.VISIBLE);
golmChartLayout.setVisibility(View.VISIBLE);
lastYear =50000f;
thisYear = 70000f;
m1=15000f;
m2=25000f;
m3=30000f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
productAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, prodList);
prodSpinner.setAdapter(productAdapter);
prodSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedProduct = prodList.get(position);
golyChartLayout.setVisibility(View.VISIBLE);
golmChartLayout.setVisibility(View.VISIBLE);
//Toast.makeText(getActivity(),selectedProduct,Toast.LENGTH_LONG).show();
switch (selectedProduct) {
case "DIAGESIC EXTRA TABS":
lastYear =2000f;
thisYear = 3000f;
m1=129f;
m2=121f;
m3=117f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "LICE-O-NIL CREAM":
lastYear = 1500f;
thisYear = 2000f;
m1=162f;
m2=193f;
m3=878f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "MYTIL 125 MG SUSPENSION":
lastYear =2500f;
thisYear = 3500f;
m1=289f;
m2=326f;
m3=380f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "NIB 2.5 MG TABS":
lastYear =1750f;
thisYear = 2000f;
m1=861f;
m2=143f;
m3=1242f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "POLYPEP 20MG TABS":
lastYear =1000f;
thisYear = 1500f;
m1=935f;
m2=116f;
m3=982f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "SOFVASC 5 MG TABS":
lastYear =500f;
thisYear = 700f;
m1=490f;
m2=110f;
m3=431f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "WILGESIC TABS":
lastYear =700f;
thisYear = 1000f;
m1=219f;
m2=235f;
m3=205f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "GENTLE 10 MG TABS":
lastYear =550f;
thisYear = 750f;
m1=224f;
m2=346f;
m3=110f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "NIB 5 MG TABS":
lastYear =2500f;
thisYear = 3100f;
m1=269f;
m2=297f;
m3=222f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
case "SAVE 10 MG TABS":
lastYear =2100f;
thisYear = 2300f;
m1=470f;
m2=487f;
m3=369f;
golyChartView(lastYear,thisYear);
golmChartView(m1,m2,m3);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void golyChartView(Number prev,Number curr)
{
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("bar");
options.setChart(chart);
HITitle title = new HITitle();
title.setText("GOLY");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Growth Over Last Year");
options.setSubtitle(subtitle);
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "2020", "2021"};
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("Sale Amount");
yaxis.getTitle().setAlign("high");
yaxis.setLabels(new HILabels());
yaxis.getLabels().setOverflow("justify");
options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});
HITooltip tooltip = new HITooltip();
options.setTooltip(tooltip);
HILegend legend = new HILegend();
legend.setLayout("vertical");
legend.setAlign("right");
legend.setVerticalAlign("top");
legend.setX(-30);
legend.setY(40);
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("Sale Value");
Number[] bar1Data = new Number[]{prev,curr};
bar1.setColorByPoint(true);
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
//Float difference = bar1Data[1] - bar1Data[0];
HIDataLabels dataLabels = new HIDataLabels();
dataLabels.setEnabled(true);
ArrayList<HIDataLabels> dataLabelsList = new ArrayList<>();
dataLabelsList.add(dataLabels);
bar1.setDataLabels(dataLabelsList);
HIDataLabels hiDataLabels = new HIDataLabels();
hiDataLabels.setEnabled(true);
HILegend hiLegend = new HILegend();
hiLegend.setEnabled(false);
options.setLegend(hiLegend);
options.setSeries(new ArrayList<>(Collections.singletonList(bar1)));
golyChart.setOptions(options);
}
public void golmChartView(Number mon1,Number mon2,Number mon3)
{
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("bar");
options.setChart(chart);
HITitle title = new HITitle();
title.setText("GOLM");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Growth Over Last Month");
options.setSubtitle(subtitle);
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);}});
HIYAxis yaxis = new HIYAxis();
yaxis.setMin(0);
yaxis.setTitle(new HITitle());
yaxis.getTitle().setText("Sale Amount");
yaxis.getTitle().setAlign("high");
yaxis.setLabels(new HILabels());
yaxis.getLabels().setOverflow("justify");
options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});
HITooltip tooltip = new HITooltip();
options.setTooltip(tooltip);
HILegend legend = new HILegend();
legend.setLayout("vertical");
legend.setAlign("right");
legend.setVerticalAlign("top");
legend.setX(-30);
legend.setY(40);
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("Sale Value");
Number[] bar1Data = new Number[]{mon1,mon2,mon3};
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
bar1.setColorByPoint(true);
HIDataLabels dataLabels = new HIDataLabels();
dataLabels.setEnabled(true);
ArrayList<HIDataLabels> dataLabelsList = new ArrayList<>();
dataLabelsList.add(dataLabels);
bar1.setDataLabels(dataLabelsList);
HILegend hiLegend = new HILegend();
hiLegend.setEnabled(false);
options.setLegend(hiLegend);
options.setSeries(new ArrayList<>(Collections.singletonList(bar1)));
golmChart.setOptions(options);
}
}
现在,在执行我的代码后,如果我选择Group Wise
类别,那么我可以看到相应的图表。之后,如果我选择该选项Product Wise
,则图表将不可见。在选择产品时,图表再次可见,但显示的值Group wise
不是按产品显示的。
输出
简而言之,我希望每当我选择一个类别或具有新值的产品图表时都应该呈现。
更新
我已经尝试过了chart.redraw()
,chart.invalidate()
但在我的情况下似乎没有任何效果
任何帮助将不胜感激。