简体   繁体   English

如何在我的 Android 项目中实现 anychart?

[英]How do I implement anychart into my Android project?

I wanted to use AnyChart's Line Chart to draw data from Firebase's realtime db but I can't seem to make it work... I have been trying to use the sample code which is given and it can't seem to appear, the code seems to compile fine but the chart does not appear... Can't seem to find what's going on.我想使用 AnyChart 的折线图从 Firebase 的实时数据库中绘制数据,但我似乎无法让它工作......我一直在尝试使用给出的示例代码,但它似乎无法出现,代码似乎编译正常,但图表没有出现......似乎无法找到发生了什么。 I can expect it to be a very simple error, but my knowledge with android is really basic and with AnyChart is null...我可以预期这是一个非常简单的错误,但我对 android 的了解非常基础,而 AnyChart 则为空...

Here you have the activity were I'm trying to implement the sample of the linear chart:在这里,您有我正在尝试实施线性图表示例的活动:

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.anychart.charts.Cartesian;
import com.anychart.core.cartesian.series.Line;
import com.anychart.data.Mapping;
import com.anychart.data.Set;
import com.anychart.enums.Anchor;
import com.anychart.enums.MarkerType;
import com.anychart.enums.TooltipPositionMode;
import com.anychart.graphics.vector.Stroke;
import java.util.ArrayList;
import java.util.List;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Activity2 extends AppCompatActivity {

    private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private int fieldNum;
    TextView tvFieldNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);


        Intent cameFromAct1=getIntent();
        fieldNum = Integer.parseInt(cameFromAct1.getStringExtra("fieldNum"));
        String fieldName = "Campo"+fieldNum+"".trim();
        //tvFieldNum=(TextView)findViewById(R.id.tvFieldNum);
        //tvFieldNum.setText(fieldNum+"");


            AnyChartView anyChartView = findViewById(R.id.any_chart_view);
            anyChartView.setProgressBar(findViewById(R.id.progress_bar));

            Cartesian cartesian = AnyChart.line();

            cartesian.animation(true);

            cartesian.padding(10d, 20d, 5d, 20d);

            cartesian.crosshair().enabled(true);
            cartesian.crosshair()
                    .yLabel(true)
                    .yStroke("#000000",1.5,"10.5","1","1");
                    //.yStroke((Stroke) null, 1.5, null, (String) null, (String) null);

            cartesian.tooltip().positionMode(TooltipPositionMode.POINT);

            cartesian.title("Trend of Sales of the Most Popular Products of ACME Corp.");

            cartesian.yAxis(0).title("Number of Bottles Sold (thousands)");
            cartesian.xAxis(0).labels().padding(5d, 5d, 5d, 5d);

            List<DataEntry> seriesData = new ArrayList<>();
            seriesData.add(new CustomDataEntry("1986", 3.6, 2.3, 2.8));
            seriesData.add(new CustomDataEntry("1987", 7.1, 4.0, 4.1));
            seriesData.add(new CustomDataEntry("1988", 8.5, 6.2, 5.1));
            seriesData.add(new CustomDataEntry("1989", 9.2, 11.8, 6.5));
            seriesData.add(new CustomDataEntry("1990", 10.1, 13.0, 12.5));
            seriesData.add(new CustomDataEntry("1991", 11.6, 13.9, 18.0));
            seriesData.add(new CustomDataEntry("1992", 16.4, 18.0, 21.0));
            seriesData.add(new CustomDataEntry("1993", 18.0, 23.3, 20.3));
            seriesData.add(new CustomDataEntry("1994", 13.2, 24.7, 19.2));
            seriesData.add(new CustomDataEntry("1995", 12.0, 18.0, 14.4));
            seriesData.add(new CustomDataEntry("1996", 3.2, 15.1, 9.2));
            seriesData.add(new CustomDataEntry("1997", 4.1, 11.3, 5.9));
            seriesData.add(new CustomDataEntry("1998", 6.3, 14.2, 5.2));
            seriesData.add(new CustomDataEntry("1999", 9.4, 13.7, 4.7));
            seriesData.add(new CustomDataEntry("2000", 11.5, 9.9, 4.2));
            seriesData.add(new CustomDataEntry("2001", 13.5, 12.1, 1.2));
            seriesData.add(new CustomDataEntry("2002", 14.8, 13.5, 5.4));
            seriesData.add(new CustomDataEntry("2003", 16.6, 15.1, 6.3));
            seriesData.add(new CustomDataEntry("2004", 18.1, 17.9, 8.9));
            seriesData.add(new CustomDataEntry("2005", 17.0, 18.9, 10.1));
            seriesData.add(new CustomDataEntry("2006", 16.6, 20.3, 11.5));
            seriesData.add(new CustomDataEntry("2007", 14.1, 20.7, 12.2));
            seriesData.add(new CustomDataEntry("2008", 15.7, 21.6, 10));
            seriesData.add(new CustomDataEntry("2009", 12.0, 22.5, 8.9));

            Set set = Set.instantiate();
            set.data(seriesData);
            Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
            Mapping series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");
            Mapping series3Mapping = set.mapAs("{ x: 'x', value: 'value3' }");

            Line series1 = cartesian.line(series1Mapping);
            series1.name("Brandy");
            series1.hovered().markers().enabled(true);
            series1.hovered().markers()
                    .type(MarkerType.CIRCLE)
                    .size(4d);
            series1.tooltip()
                    .position("right")
                    .anchor(Anchor.LEFT_CENTER)
                    .offsetX(5d)
                    .offsetY(5d);

            Line series2 = cartesian.line(series2Mapping);
            series2.name("Whiskey");
            series2.hovered().markers().enabled(true);
            series2.hovered().markers()
                    .type(MarkerType.CIRCLE)
                    .size(4d);
            series2.tooltip()
                    .position("right")
                    .anchor(Anchor.LEFT_CENTER)
                    .offsetX(5d)
                    .offsetY(5d);

            Line series3 = cartesian.line(series3Mapping);
            series3.name("Tequila");
            series3.hovered().markers().enabled(true);
            series3.hovered().markers()
                    .type(MarkerType.CIRCLE)
                    .size(4d);
            series3.tooltip()
                    .position("right")
                    .anchor(Anchor.LEFT_CENTER)
                    .offsetX(5d)
                    .offsetY(5d);

            cartesian.legend().enabled(true);
            cartesian.legend().fontSize(13d);
            cartesian.legend().padding(0d, 0d, 10d, 0d);

            anyChartView.setChart(cartesian);
        }

        private class CustomDataEntry extends ValueDataEntry {

            CustomDataEntry(String x, Number value, Number value2, Number value3) {
                super(x, value);
                setValue("value2", value2);
                setValue("value3", value3);
            }

        }

    }

And here you have my XML file from that activity在这里你有来自那个活动的我的 XML 文件

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2"
    android:background="@drawable/background">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <com.anychart.AnyChartView
                android:layout_weight="1"
                android:id="@+id/any_chart_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <ProgressBar
                android:layout_weight="1"
                android:id="@+id/progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>```

There's a lack of some attributes in the XML, also try to not use ScrollView. XML 中缺少一些属性,也尽量不要使用 ScrollView。 As reference, check the XML snippet and activity code in the Quick Start Guide .作为参考,请检查快速入门指南中的 XML 片段和活动代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM