Search in sources :

Example 11 with GraphView

use of com.jjoe64.graphview.GraphView in project apneil by Inv4si0n.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GraphView graph = findViewById(R.id.graph);
    // Titre du graph
    graph.setTitle("Titre");
    // Couleur du titre du graph
    graph.setTitleColor(Color.BLUE);
    series = new LineGraphSeries<>(generateData());
    // Couleur de la courbe
    series.setColor(Color.GREEN);
    // Tracé les points
    series.setDrawDataPoints(true);
    // Radius points
    series.setDataPointsRadius(10);
    // Epaisseur
    series.setThickness(2);
    graph.addSeries(series);
    Button button = findViewById(R.id.on);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            System.out.println("ta mere la chauve");
        }
    });
}
Also used : Button(android.widget.Button) GraphView(com.jjoe64.graphview.GraphView) View(android.view.View) GraphView(com.jjoe64.graphview.GraphView)

Example 12 with GraphView

use of com.jjoe64.graphview.GraphView in project netxms by netxms.

the class DrawGraph method onCreateStep2.

/* (non-Javadoc)
	 * @see org.netxms.ui.android.main.activities.AbstractClientActivity#onCreateStep2(android.os.Bundle)
	 */
@Override
protected void onCreateStep2(Bundle savedInstanceState) {
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    dialog = new ProgressDialog(this);
    setContentView(R.layout.graphics);
    // boolean showLegend = getIntent().getBooleanExtra("showLegend", true);
    numGraphs = getIntent().getIntExtra("numGraphs", 0);
    if (numGraphs > 0) {
        items = new GraphItem[numGraphs];
        itemStyles = new GraphItemStyle[numGraphs];
        ArrayList<Integer> nodeIdList = getIntent().getIntegerArrayListExtra("nodeIdList");
        ArrayList<Integer> dciIdList = getIntent().getIntegerArrayListExtra("dciIdList");
        ArrayList<Integer> colorList = getIntent().getIntegerArrayListExtra("colorList");
        ArrayList<Integer> lineWidthList = getIntent().getIntegerArrayListExtra("lineWidthList");
        ArrayList<String> nameList = getIntent().getStringArrayListExtra("nameList");
        for (int i = 0; i < numGraphs; i++) {
            items[i] = new GraphItem();
            items[i].setNodeId(nodeIdList.get(i));
            items[i].setDciId(dciIdList.get(i));
            items[i].setDescription(nameList.get(i));
            itemStyles[i] = new GraphItemStyle();
            itemStyles[i].setColor(colorList.get(i) | 0xFF000000);
            itemStyles[i].setLineWidth(lineWidthList.get(i));
        }
        timeFrom = getIntent().getLongExtra("timeFrom", 0);
        timeTo = getIntent().getLongExtra("timeTo", 0);
        graphTitle = getIntent().getStringExtra("graphTitle");
    }
    graphView = new LineGraphView(this, "");
    graphView.getGraphViewStyle().setVerticalLabelsColor(Color.WHITE);
    graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.WHITE);
    graphView.getGraphViewStyle().setTextSize(Integer.parseInt(sp.getString("global.graph.textsize", "10")));
    graphView.getGraphViewStyle().setLegendWidth(240);
    graphView.setCustomLabelFormatter(new CustomLabel(Integer.parseInt(sp.getString("global.multipliers", "1")), (timeTo - timeFrom) > 86400 * 1000));
    // TOOD: 2014May25 Find a best way to handle this setting
    // graphView.setShowLegend(showLegend);
    graphView.setShowLegend(sp.getBoolean("global.graph.legend", true));
    graphView.setLegendAlign(LegendAlign.TOP);
    graphView.setScalable(true);
    graphView.setScrollable(true);
    TextView title = (TextView) findViewById(R.id.ScreenTitlePrimary);
    title.setText(R.string.graph_title);
}
Also used : LineGraphView(com.jjoe64.graphview.LineGraphView) CustomLabel(org.netxms.ui.android.helpers.CustomLabel) GraphItem(org.netxms.client.datacollection.GraphItem) GraphItemStyle(org.netxms.client.datacollection.GraphItemStyle) TextView(android.widget.TextView) ProgressDialog(android.app.ProgressDialog)

Example 13 with GraphView

use of com.jjoe64.graphview.GraphView in project netxms by netxms.

the class LineChartElement method refresh.

/* (non-Javadoc)
	 * @see org.netxms.ui.android.main.dashboards.elements.AbstractDashboardElement#refresh()
	 */
@Override
public void refresh() {
    final ChartDciConfig[] items = config.getDciList();
    Log.v(TAG, "refresh(): " + items.length + " items to load");
    if (items.length == 0)
        return;
    final long endTime = System.currentTimeMillis();
    final long startTime = endTime - config.getTimeRangeMillis();
    graphView.setCustomLabelFormatter(new CustomLabel(Integer.parseInt(sp.getString("global.multipliers", "1")), (endTime - startTime) > 86400 * 1000));
    try {
        final DciData[] dciData = new DciData[items.length];
        for (int i = 0; i < dciData.length; i++) {
            dciData[i] = service.getSession().getCollectedData(items[i].nodeId, items[i].dciId, new Date(startTime), new Date(endTime), 0);
        }
        Log.v(TAG, "refresh(): data retrieved from server");
        post(new Runnable() {

            @Override
            public void run() {
                graphView.removeAllSeries();
                for (int i = 0; i < dciData.length && i < Colors.DEFAULT_ITEM_COLORS.length; i++) {
                    DciDataRow[] dciDataRow = dciData[i].getValues();
                    GraphViewData[] gvData = new GraphViewData[dciDataRow.length];
                    for (int j = dciDataRow.length - 1, k = 0; j >= 0; j--, k++) // dciData are reversed!
                    gvData[k] = new GraphViewData(dciDataRow[j].getTimestamp().getTime(), dciDataRow[j].getValueAsDouble());
                    int color = items[i].getColorAsInt();
                    color = color == -1 ? Colors.DEFAULT_ITEM_COLORS[i] : swapRGB(color);
                    GraphViewSeries series = new GraphViewSeries(items[i].getName(), new GraphViewSeriesStyle(color | 0xFF000000, 3), gvData);
                    graphView.addSeries(series);
                }
                graphView.setViewPort(startTime, endTime - startTime + 1);
                Log.v(TAG, "refresh(): " + dciData.length + " series added; viewport set to " + startTime + "/" + (endTime - startTime + 1));
                if (getChildCount() == 0)
                    addView(graphView);
                else
                    graphView.redrawAll();
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Exception while reading data from server", e);
    }
}
Also used : CustomLabel(org.netxms.ui.android.helpers.CustomLabel) ChartDciConfig(org.netxms.ui.android.main.activities.helpers.ChartDciConfig) DciData(org.netxms.client.datacollection.DciData) Date(java.util.Date) GraphViewSeries(com.jjoe64.graphview.GraphViewSeries) GraphViewSeriesStyle(com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle) GraphViewData(com.jjoe64.graphview.GraphView.GraphViewData)

Example 14 with GraphView

use of com.jjoe64.graphview.GraphView in project GraphView by appsthatmatter.

the class GraphView method drawGraphElements.

/**
 * draw all the stuff on canvas
 *
 * @param canvas
 */
protected void drawGraphElements(Canvas canvas) {
    // must be in hardware accelerated mode
    if (android.os.Build.VERSION.SDK_INT >= 11 && !canvas.isHardwareAccelerated()) {
        // just warn about it, because it is ok when making a snapshot
        Log.w("GraphView", "GraphView should be used in hardware accelerated mode." + "You can use android:hardwareAccelerated=\"true\" on your activity. Read this for more info:" + "https://developer.android.com/guide/topics/graphics/hardware-accel.html");
    }
    drawTitle(canvas);
    mViewport.drawFirst(canvas);
    mGridLabelRenderer.draw(canvas);
    for (Series s : mSeries) {
        s.draw(this, canvas, false);
    }
    if (mSecondScale != null) {
        for (Series s : mSecondScale.getSeries()) {
            s.draw(this, canvas, true);
        }
    }
    if (mCursorMode != null) {
        mCursorMode.draw(canvas);
    }
    mViewport.draw(canvas);
    mLegendRenderer.draw(canvas);
}
Also used : BaseSeries(com.jjoe64.graphview.series.BaseSeries) Series(com.jjoe64.graphview.series.Series)

Example 15 with GraphView

use of com.jjoe64.graphview.GraphView in project GraphView by appsthatmatter.

the class BarGraphSeries method draw.

/**
 * draws the bars on the canvas
 *
 * @param graphView corresponding graphview
 * @param canvas canvas
 * @param isSecondScale whether we are plotting the second scale or not
 */
@Override
public void draw(GraphView graphView, Canvas canvas, boolean isSecondScale) {
    mPaint.setTextAlign(Paint.Align.CENTER);
    if (mValuesOnTopSize == 0) {
        mValuesOnTopSize = graphView.getGridLabelRenderer().getTextSize();
    }
    mPaint.setTextSize(mValuesOnTopSize);
    resetDataPoints();
    // get data
    double maxX = graphView.getViewport().getMaxX(false);
    double minX = graphView.getViewport().getMinX(false);
    double maxY;
    double minY;
    if (isSecondScale) {
        maxY = graphView.getSecondScale().getMaxY(false);
        minY = graphView.getSecondScale().getMinY(false);
    } else {
        maxY = graphView.getViewport().getMaxY(false);
        minY = graphView.getViewport().getMinY(false);
    }
    // Iterate through all bar graph series
    // so we know how wide to make our bar,
    // and in what position to put it in
    int numBarSeries = 0;
    int currentSeriesOrder = 0;
    int numValues = 0;
    boolean isCurrentSeries;
    SortedSet<Double> xVals = new TreeSet<Double>();
    for (Series inspectedSeries : graphView.getSeries()) {
        if (inspectedSeries instanceof BarGraphSeries) {
            isCurrentSeries = (inspectedSeries == this);
            if (isCurrentSeries) {
                currentSeriesOrder = numBarSeries;
            }
            numBarSeries++;
            // calculate the number of slots for bars based on the minimum distance between
            // x coordinates in the series.  This is divided into the range to find
            // the placement and width of bar slots
            // (sections of the x axis for each bar or set of bars)
            // TODO: Move this somewhere more general and cache it, so we don't recalculate it for each series
            Iterator<E> curValues = inspectedSeries.getValues(minX, maxX);
            if (curValues.hasNext()) {
                xVals.add(curValues.next().getX());
                if (isCurrentSeries) {
                    numValues++;
                }
                while (curValues.hasNext()) {
                    xVals.add(curValues.next().getX());
                    if (isCurrentSeries) {
                        numValues++;
                    }
                }
            }
        }
    }
    if (numValues == 0) {
        return;
    }
    double minGap = 0;
    if (mDataWidth > 0.0) {
        minGap = mDataWidth;
    } else {
        Double lastVal = null;
        for (Double curVal : xVals) {
            if (lastVal != null) {
                double curGap = Math.abs(curVal - lastVal);
                if (minGap == 0 || (curGap > 0 && curGap < minGap)) {
                    minGap = curGap;
                }
            }
            lastVal = curVal;
        }
    }
    int numBarSlots = (minGap == 0) ? 1 : (int) Math.round((maxX - minX) / minGap) + 1;
    Iterator<E> values = getValues(minX, maxX);
    // Calculate the overall bar slot width - this includes all bars across
    // all series, and any spacing between sets of bars
    int barSlotWidth = numBarSlots == 1 ? graphView.getGraphContentWidth() : graphView.getGraphContentWidth() / (numBarSlots - 1);
    // Total spacing (both sides) between sets of bars
    double spacing = Math.min(barSlotWidth * mSpacing / 100, barSlotWidth * 0.98f);
    // Width of an individual bar
    double barWidth = (barSlotWidth - spacing) / numBarSeries;
    // Offset from the center of a given bar to start drawing
    double offset = barSlotWidth / 2;
    double diffY = maxY - minY;
    double diffX = maxX - minX;
    double contentHeight = graphView.getGraphContentHeight();
    double contentWidth = graphView.getGraphContentWidth();
    double contentLeft = graphView.getGraphContentLeft();
    double contentTop = graphView.getGraphContentTop();
    // draw data
    int i = 0;
    while (values.hasNext()) {
        E value = values.next();
        double valY = value.getY() - minY;
        double ratY = valY / diffY;
        double y = contentHeight * ratY;
        double valY0 = 0 - minY;
        double ratY0 = valY0 / diffY;
        double y0 = contentHeight * ratY0;
        double valueX = value.getX();
        double valX = valueX - minX;
        double ratX = valX / diffX;
        double x = contentWidth * ratX;
        // hook for value dependent color
        if (getValueDependentColor() != null) {
            mPaint.setColor(getValueDependentColor().get(value));
        } else {
            mPaint.setColor(getColor());
        }
        double left = x + contentLeft - offset + spacing / 2 + currentSeriesOrder * barWidth;
        double right = left + barWidth;
        if (left > contentLeft + contentWidth || right < contentLeft) {
            continue;
        }
        double top = (contentTop - y) + contentHeight;
        double bottom = (contentTop - y0) + contentHeight - (graphView.getGridLabelRenderer().isHighlightZeroLines() ? 4 : 1);
        boolean reverse = top > bottom;
        if (mAnimated) {
            if ((Double.isNaN(mLastAnimatedValue) || mLastAnimatedValue < valueX)) {
                long currentTime = System.currentTimeMillis();
                if (mAnimationStart == 0) {
                    // start animation
                    mAnimationStart = currentTime;
                    mAnimationStartFrameNo = 0;
                } else {
                    // anti-lag: wait a few frames
                    if (mAnimationStartFrameNo < 15) {
                        // second time
                        mAnimationStart = currentTime;
                        mAnimationStartFrameNo++;
                    }
                }
                float timeFactor = (float) (currentTime - mAnimationStart) / ANIMATION_DURATION;
                float factor = mAnimationInterpolator.getInterpolation(timeFactor);
                if (timeFactor <= 1.0) {
                    double barHeight = bottom - top;
                    barHeight = barHeight * factor;
                    top = bottom - barHeight;
                    ViewCompat.postInvalidateOnAnimation(graphView);
                } else {
                    // animation finished
                    mLastAnimatedValue = valueX;
                }
            }
        }
        if (reverse) {
            double tmp = top;
            top = bottom + (graphView.getGridLabelRenderer().isHighlightZeroLines() ? 4 : 1);
            bottom = tmp;
        }
        // overdraw
        left = Math.max(left, contentLeft);
        right = Math.min(right, contentLeft + contentWidth);
        bottom = Math.min(bottom, contentTop + contentHeight);
        top = Math.max(top, contentTop);
        mDataPoints.put(new RectD(left, top, right, bottom), value);
        Paint p;
        if (mCustomPaint != null) {
            p = mCustomPaint;
        } else {
            p = mPaint;
        }
        canvas.drawRect((float) left, (float) top, (float) right, (float) bottom, p);
        // set values on top of graph
        if (mDrawValuesOnTop) {
            if (reverse) {
                top = bottom + mValuesOnTopSize + 4;
                if (top > contentTop + contentHeight)
                    top = contentTop + contentHeight;
            } else {
                top -= 4;
                if (top <= contentTop)
                    top += contentTop + 4;
            }
            mPaint.setColor(mValuesOnTopColor);
            canvas.drawText(graphView.getGridLabelRenderer().getLabelFormatter().formatLabel(value.getY(), false), (float) (left + right) / 2, (float) top, mPaint);
        }
        i++;
    }
}
Also used : Paint(android.graphics.Paint) Paint(android.graphics.Paint) RectD(com.jjoe64.graphview.RectD) TreeSet(java.util.TreeSet)

Aggregations

GraphView (com.jjoe64.graphview.GraphView)6 GraphViewSeries (com.jjoe64.graphview.GraphViewSeries)6 GraphViewData (com.jjoe64.graphview.GraphView.GraphViewData)4 View (android.view.View)3 TextView (android.widget.TextView)3 LineGraphView (com.jjoe64.graphview.LineGraphView)3 Paint (android.graphics.Paint)2 DateAsXAxisLabelFormatter (com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter)2 DataPoint (com.jjoe64.graphview.series.DataPoint)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 CustomLabel (org.netxms.ui.android.helpers.CustomLabel)2 VisitModel (org.wordpress.android.ui.stats.models.VisitModel)2 ProgressDialog (android.app.ProgressDialog)1 BluetoothManager (android.bluetooth.BluetoothManager)1 Intent (android.content.Intent)1 Drawable (android.graphics.drawable.Drawable)1 ActionBar (android.support.v7.app.ActionBar)1 LayoutInflater (android.view.LayoutInflater)1