Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

75
LINES

< > BotCompany Repo | #1002496 // Simple Charts Bot (LIVE)

JavaX source code [tags: butter use-pretranspiled] - run with: x30.jar - homepage

Uses 180K of libraries. Click here for Pure Java version (4271L/29K/96K).

!7

lib 1001955 // charts4j

import com.googlecode.charts4j.*;
import static com.googlecode.charts4j.Color.*;
import static com.googlecode.charts4j.Shape.*;
import static com.googlecode.charts4j.UrlUtil.normalize;
import java.awt.Color;
import java.awt.Shape;

answer {
  if (jmatchStart("chart", s, m)) exceptionToUser {
    S title = s;
    int width = 300, height = 220;
    
    // Make values
    
    s = m.rest();
    print("Chart: rest = " + quote(s));
    
    if (jmatchStart("*", s, m) && isQuoted(m.get(0))) {
      title = unquote(m.get(0));
      s = m.rest();
      print("Chart: rest2 = " + quote(s));
    }

    Double base = null;
    if (jmatchStart("base *", s, m)) {
      base = parseDouble(m.unq(0));
      s = m.rest();
    }
    
    L<S> tok = asList(s.split(" +"));
    if (!empty(tok)) {
      final int NUM_POINTS = l(tok);
      final double[] values = new double[NUM_POINTS];
      for (int i = 0; i < NUM_POINTS; i++)
        values[i] = parseDouble(tok.get(i));
      print("Chart values: " + structure(values));
      double minValue = base != null ? base : min(values), maxValue = max(values);
      print("Chart: min/max=" + minValue + "/" + maxValue);
        
      Line line1 = Plots.newLine(DataUtil.scaleWithinRange(minValue, maxValue, values), com.googlecode.charts4j.Color.newColor("CA3D05"), ""/*valuesTitle*/);
      
      line1.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
      line1.addShapeMarkers(DIAMOND, newColor("CA3D05"), 12);
      line1.addShapeMarkers(DIAMOND, WHITE, 8);
      
      // Defining chart.
      
      LineChart chart = GCharts.newLineChart(line1);
      chart.setSize(width, height);
      chart.setTitle(title, BLACK, 14);
      //chart.setGrid(25, 25, 3, 2);
    
      // Adding axis info to chart.
      
      // Let's try this... works with positive values
      chart.addYAxisLabels(AxisLabelsFactory.newNumericRangeAxisLabels(minValue, maxValue));

    
      // Defining background and chart fills.
      
      /*
      chart.setBackgroundFill(Fills.newSolidFill(newColor("1F1D1D")));
      LinearGradientFill fill = Fills.newLinearGradientFill(0, newColor("363433"), 100);
      fill.addColorAndOffset(newColor("2E2B2A"), 0);
      chart.setAreaFill(fill);
      */
      
      ret returnImage(chart.toURLString());
    }
  }
}

Author comment

Began life as a copy of #1002471

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xfasiavlzxuf

No comments. add comment

Snippet ID: #1002496
Snippet name: Simple Charts Bot (LIVE)
Eternal ID of this version: #1002496/3
Text MD5: cd5efde3468e29a384466d4bc7acc04c
Transpilation MD5: 215054d786ae2e2a0faa2e4f6ecd734c
Author: stefan
Category: eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-04-09 12:01:41
Source code size: 2391 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 711 / 2353
Version history: 2 change(s)
Referenced in: [show references]