Exemplo n.º 1
0
 /** {@inheritDoc} */
 public String toURLParameterString() {
   final StringBuffer sb = new StringBuffer(URL_PARAMETER_KEY + "=" + dataEncoding);
   final String seperator = dataEncoding.equals(DataEncoding.TEXT) ? "|" : ",";
   int cnt = 0;
   for (Data data : datas) {
     final String dataString =
         dataEncoding.equals(DataEncoding.SIMPLE)
             ? simpleEncoding(data.getData())
             : dataEncoding.equals(DataEncoding.TEXT)
                 ? textEncoding(data.getData())
                 : extendedEncoding(data.getData());
     sb.append(cnt++ > 0 ? seperator + dataString : dataString);
   }
   return sb.toString();
 }
  @RequestMapping(value = "/Regression", method = RequestMethod.POST)
  public String regression(
      ModelMap model,
      HttpServletRequest request,
      @Valid stocksearchhelper StockSearchHelper,
      BindingResult result)
      throws MalformedURLException, IOException {

    if (result.hasErrors()) {
      model.addAttribute("re", "Please Check the Values Entered");
      model.addAttribute("StockSearchHelper", new stocksearchhelper());
      model.addAttribute("CompanyName", new CompanyReports());
      List<stock> x = stockServiceInterface.liststock("NASDAQ");
      List<stock> y = stockServiceInterface.liststock("NASDAQCOMPOSITE");
      List<stock> z = stockServiceInterface.liststock("DOWJONES");
      List<stock> a = stockServiceInterface.liststock("GOLD");
      List<stock> b = stockServiceInterface.liststock("SANDP");
      List<stock> c = stockServiceInterface.liststock("OIL");
      model.addAttribute("NASDAQ", x.get(0).getPrice());
      model.addAttribute("NASDAQCOMPOSITE", y.get(0).getPrice());
      model.addAttribute("DOWJONES", z.get(0).getPrice());
      model.addAttribute("GOLD", a.get(0).getPrice());
      model.addAttribute("SANDP", b.get(0).getPrice());
      model.addAttribute("OIL", c.get(0).getPrice());
      List<TopPerformers> opds1 = topPerformerServiceInterface.liststock();
      model.addAttribute("first", opds1.get(0).getTickerCode());
      model.addAttribute("second", opds1.get(1).getTickerCode());
      model.addAttribute("third", opds1.get(2).getTickerCode());
      model.addAttribute("fourth", opds1.get(3).getTickerCode());
      return "ClientHome";
    } else {
      String symbol = StockSearchHelper.getName();
      statistics s = statisticServiceInterface.refreshStockInfo(symbol);
      // System.out.println("apple"+s.getRsquare());
      double[] x = s.getX();

      double[] y = new double[20];

      for (int i = 0; i < y.length; i++) {

        y[i] = x[i] / 10;
      }

      // Data data = Data.newData(y);

      Line line1 = Plots.newLine(Data.newData(y), Color.newColor("CA3D05"), "Trend");
      line1.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
      line1.addShapeMarkers(Shape.DIAMOND, Color.newColor("CA3D05"), 12);
      line1.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 8);

      LineChart chart = GCharts.newLineChart(line1);
      chart.setSize(600, 450);
      chart.setTitle("Market Trend|(in 100's)", Color.WHITE, 14);

      AxisLabels yAxis = AxisLabelsFactory.newAxisLabels("", "1", "2", "3", "4");
      AxisLabels xAxis3 = AxisLabelsFactory.newAxisLabels("Weeks", 25.0);
      xAxis3.setAxisStyle(AxisStyle.newAxisStyle(Color.WHITE, 14, AxisTextAlignment.CENTER));
      yAxis.setAxisStyle(AxisStyle.newAxisStyle(Color.WHITE, 14, AxisTextAlignment.CENTER));

      chart.setBackgroundFill(Fills.newSolidFill(Color.newColor("1F1D1D")));
      LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.newColor("363433"), 100);
      fill.addColorAndOffset(Color.newColor("2E2B2A"), 0);
      chart.setAreaFill(fill);

      chart.addXAxisLabels(yAxis);
      chart.addXAxisLabels(xAxis3);

      String url = chart.toURLString();

      // Passing the chart to the JSP for rendering.
      model.addAttribute("url", chart.toURLString());
      model.addAttribute("Statistics", s);

      long currentTime = (new Date()).getTime();
      // long

      try {

        URL yahoofin =
            new URL("http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=e7e8e9r6r7&e=.csv");
        URLConnection yc = yahoofin.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
          String[] yahooStockInfo = inputLine.split(",");

          model.addAttribute("epsestimatecurrentyear", Float.valueOf(yahooStockInfo[0]));
          model.addAttribute("epsestimatenextyear", Float.valueOf(yahooStockInfo[1]));
          model.addAttribute("epsestimatenextquarter", Float.valueOf(yahooStockInfo[2]));
          model.addAttribute("pricesestimatecurrentyear", Float.valueOf(yahooStockInfo[3]));
          model.addAttribute("priceestimatenextyear", Float.valueOf(yahooStockInfo[4]));

          break;
        }
        in.close();
        return "statisticanalysis";

      } catch (Exception ex) {

      }

      return "statisticanalysis";
    }
  }
  public String draw(String title, Map<String, List<Double>> data, List<String> xLabels) {

    /*
        Scale values
    */
    Double maxValue = Double.MIN_VALUE;
    Double minValue = Double.MAX_VALUE;
    for (List<Double> doubles : data.values()) {
      for (Double d : doubles) {
        maxValue = Math.max(d, maxValue);
        minValue = Math.min(d, minValue);
      }
    }
    double k = 100.0 / maxValue;
    HashMap<String, List<Double>> scaled = new HashMap<>();
    for (Map.Entry<String, List<Double>> entry : data.entrySet()) {
      List<Double> value = new ArrayList<>();
      for (Double next : entry.getValue()) {
        value.add(next * k);
      }
      scaled.put(entry.getKey(), value);
    }
    data = scaled;

    Random rnd = new Random(42);

    // Defining lines
    ArrayList<Line> lines = new ArrayList<>();
    for (Map.Entry<String, List<Double>> entry : data.entrySet()) {
      Color color =
          Color.newColor(
              String.format("%02X%02X%02X", rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
      Line line = Plots.newLine(Data.newData(entry.getValue()), color, entry.getKey());
      line.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
      line.addShapeMarkers(Shape.DIAMOND, color, 12);
      line.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 8);
      lines.add(line);
    }

    // Defining chart.
    LineChart chart = GCharts.newLineChart(lines);
    chart.setSize(600, 450);
    chart.setTitle(title, WHITE, 14);
    int stepCount = 5;
    chart.setGrid(100 / stepCount, 100 / stepCount, 3, 3);

    // Defining axis info and styles

    // Make x axis
    AxisStyle axisStyle = AxisStyle.newAxisStyle(WHITE, 12, AxisTextAlignment.CENTER);
    AxisLabels xAxis = AxisLabelsFactory.newAxisLabels(xLabels);
    xAxis.setAxisStyle(axisStyle);

    // Calculate y axis labels
    List<String> yLabels = new ArrayList<>();
    for (int i = 0; i <= stepCount; i++) {
      yLabels.add(String.valueOf(maxValue / stepCount * i));
    }
    AxisLabels yAxis = AxisLabelsFactory.newAxisLabels(yLabels);
    yAxis.setAxisStyle(axisStyle);

    // Adding axis info to chart.
    chart.addXAxisLabels(xAxis);
    chart.addYAxisLabels(yAxis);

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

    return chart.toURLString();
  }