【问题标题】:ANYCHART LinearColorScaleANYCHART 线性颜色刻度
【发布时间】:2021-12-15 18:03:03
【问题描述】:

我正在尝试构建 AnyChart 的 linearColorScale,我尝试了下面的代码,但没有得到任何结果。如果此代码中存在错误或我遗漏了什么,请告诉我。

附:如前所述,我修改了 minSDK 19 并遵循了文档。如果我遗漏了什么,请告诉我:)

TIA。

   public class GeneralFragment extends Fragment {

    AnyChartView daysScale;
    private View view;
    private LineChart symbolDetailChart;
    private ArrayList<Entry> values;
    private LineDataSet set1;
    private LineData data;
    private Drawable drawablePositive = ContextCompat.getDrawable(Logs.globalContext, R.drawable.graph_green_bg);



    public GeneralFragment() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_general, container, false);

        daysScale = view.findViewById(R.id.scalechartdays);
       
        LinearGauge linearGauge = AnyChart.linear();

        linearGauge.data(new SingleValueDataSet(new Double[] { 5.3D }));

        linearGauge.layout(Layout.HORIZONTAL);

        linearGauge.label(0)
                .position(Position.LEFT_CENTER)
                .anchor(Anchor.LEFT_CENTER)
                .offsetY("-50px")
                .offsetX("50px")
                .fontColor("black")
                .fontSize(17);
        linearGauge.label(0).text("Total Rainfall");

        linearGauge.label(1)
                .position(Position.LEFT_CENTER)
                .anchor(Anchor.LEFT_CENTER)
                .offsetY("40px")
                .offsetX("50px")
                .fontColor("#777777")
                .fontSize(17);
        linearGauge.label(1).text("Drought Hazard");

        linearGauge.label(2)
                .position(Position.RIGHT_CENTER)
                .anchor(Anchor.RIGHT_CENTER)
                .offsetY("40px")
                .offsetX("50px")
                .fontColor("#777777")
                .fontSize(17);
        linearGauge.label(2).text("Flood Hazard");

        OrdinalColor scaleBarColorScale = OrdinalColor.instantiate();
        scaleBarColorScale.ranges(new String[]{
                "{ from: 0, to: 2, color: ['red 0.5'] }",
                "{ from: 2, to: 3, color: ['yellow 0.5'] }",
                "{ from: 3, to: 7, color: ['green 0.5'] }",
                "{ from: 7, to: 8, color: ['yellow 0.5'] }",
                "{ from: 8, to: 10, color: ['red 0.5'] }"
        });

        linearGauge.scaleBar(0)
                .width("5%")
                .colorScale(scaleBarColorScale);

        linearGauge.marker(0)
                .type(MarkerType.TRIANGLE_DOWN)
                .color("red")
                .offset("-3.5%")
                .zIndex(10);

        linearGauge.scale()
                .minimum(0)
                .maximum(10);
//        linearGauge.scale().ticks

        linearGauge.axis(0)
                .minorTicks(false)
                .width("1%");
        linearGauge.axis(0)
                .offset("-1.5%")
                .orientation(Orientation.TOP)
                .labels("top");

        linearGauge.padding(0, 30, 0, 30);

        daysScale.setChart(linearGauge);

       

        return  view;
    }
}

【问题讨论】:

    标签: java android java-me anychart


    【解决方案1】:

    图表配置代码绝对ok。下面是工作代码和结果图表。您应该检查错误并调试其余代码。

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            AnyChartView anyChartView = findViewById(R.id.any_chart_view);
    
            LinearGauge linearGauge = AnyChart.linear();
    
            linearGauge.data(new SingleValueDataSet(new Double[] { 5.3D }));
    
            linearGauge.layout(Layout.HORIZONTAL);
    
            linearGauge.label(0)
                    .position(Position.LEFT_CENTER)
                    .anchor(Anchor.LEFT_CENTER)
                    .offsetY("-50px")
                    .offsetX("50px")
                    .fontColor("black")
                    .fontSize(17);
            linearGauge.label(0).text("Total Rainfall");
    
            linearGauge.label(1)
                    .position(Position.LEFT_CENTER)
                    .anchor(Anchor.LEFT_CENTER)
                    .offsetY("40px")
                    .offsetX("50px")
                    .fontColor("#777777")
                    .fontSize(17);
            linearGauge.label(1).text("Drought Hazard");
    
            linearGauge.label(2)
                    .position(Position.RIGHT_CENTER)
                    .anchor(Anchor.RIGHT_CENTER)
                    .offsetY("40px")
                    .offsetX("50px")
                    .fontColor("#777777")
                    .fontSize(17);
            linearGauge.label(2).text("Flood Hazard");
    
            OrdinalColor scaleBarColorScale = OrdinalColor.instantiate();
            scaleBarColorScale.ranges(new String[]{
                    "{ from: 0, to: 2, color: ['red 0.5'] }",
                    "{ from: 2, to: 3, color: ['yellow 0.5'] }",
                    "{ from: 3, to: 7, color: ['green 0.5'] }",
                    "{ from: 7, to: 8, color: ['yellow 0.5'] }",
                    "{ from: 8, to: 10, color: ['red 0.5'] }"
            });
    
            linearGauge.scaleBar(0)
                    .width("5%")
                    .colorScale(scaleBarColorScale);
    
            linearGauge.marker(0)
                    .type(MarkerType.TRIANGLE_DOWN)
                    .color("red")
                    .offset("-3.5%")
                    .zIndex(10);
    
            linearGauge.scale()
                    .minimum(0)
                    .maximum(10);
    //        linearGauge.scale().ticks
    
            linearGauge.axis(0)
                    .minorTicks(false)
                    .width("1%");
            linearGauge.axis(0)
                    .offset("-1.5%")
                    .orientation(Orientation.TOP)
                    .labels("top");
    
            linearGauge.padding(0, 30, 0, 30);
    
            anyChartView.setChart(linearGauge);
        }
    

    【讨论】:

    • 它在活动中运行良好,但在片段中运行良好。您附加的代码和屏幕截图是活动的,我正在片段内工作。
    • 当前版本的库不支持 Fragment。
    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多