Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a trick to using d3 charts in jsf?

I am using Prime Faces in Liferay, What is the trick to getting the D3 examples to show up in jsf land specifically Prime Faces on Liferay. The example I am trying to make work in JSF is this one: How would d3.js difference chart example work with json data?

like image 376
John J. Ryan Avatar asked May 08 '13 20:05

John J. Ryan


People also ask

What is D3 explain about the big data visualization using D3 with example?

D3. js is a JavaScript library for creating visualizations like charts, maps, and more on the web. Unlike many other data visualization libraries that provide ready made charts, D3 gives you lots of creative freedom as you have total control over the visualizations you create.

How do you make a bar chart in D3?

The first step to create a bar chart is to define the area in which the chart will be drawn. We do this by specifying the height and width parameters for the SVG container. Remember the graphics we will create are all SVG. So the width is set to 600 pixels and height is set to 250 pixels. What are scales in D3?

How to improve the Order of the results in D3?

Regarding the order of the bar, the graph can be improved by ordering all the results into a descending order according to the value field. In D3 this can be achieved through the sort function: This function requires as input parameter a function which specifies the rule used to order every pair of fields.

How to sort a graph with no focus in D3?

In D3 this can be achieved through the sort function: This function requires as input parameter a function which specifies the rule used to order every pair of fields. Regarding the fact that the graph has no focus, I can focus on a single country, such as France, and represent it with a different color with respect to the other countries.

What is the best JavaScript library for drawing graphs?

The Data Driven Library (D3) is one of the most famous Javascript libraries used to build graphs and draw paintings. Regarding graphs, there are many web sites providing ready-to-use code, such as the D3 Graph Gallery and From Data to Viz.


1 Answers

I figured it out. The trick is to reference a div element instead of body. All of the examples I have seen use "body".In a portal container you don't want to use body as that will put you outside of your port let. A div is required and must be referenced as in the javascript. I have included the new code below

    <%@ page import="javax.portlet.WindowState" %>
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>


    <portlet:defineObjects />

    <form action="<portlet:actionURL />" method="post" name="<portlet:namespace />fm">
    <div id="svgContainer" ></div>
    <script type="text/javascript" >
    var margin = {top: 20, right: 20, bottom: 30, left: 50},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    var parseDate = d3.time.format("%Y%m%d").parse;

    var x = d3.time.scale()
        .range([0, width]);

    var y = d3.scale.linear()
        .range([height, 0]);

    var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom");

    var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left");

    var line = d3.svg.area()
        .interpolate("basis")
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(d["student"]); });

    var area = d3.svg.area()
        .interpolate("basis")
        .x(function(d) { return x(d.date); })
        .y1(function(d) { return y(d["student"]); });

    var svg = d3.select("#svgContainer").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    var count =0;

    d3.json("<%=request.getContextPath()%>/data/data.json", function(error, data) {
      if(error) return console.warn(error);
          data.data.forEach(function(d) {
            d.date = parseDate(d.date);
            d["student"]= +d["student"];
            d["average"] = +d["average"];
            count++;

          });

      x.domain(d3.extent(data.data, function(d) { return d.date; }));

      y.domain([
        d3.min(data.data, function(d) { return Math.min(d["student"], d["average"]); }),
        d3.max(data.data, function(d) { return Math.max(d["student"], d["average"]); })
      ]);

      svg.datum(data.data);

      svg.append("clipPath")
          .attr("id", "clip-below")
        .append("path")
          .attr("d", area.y0(height));

      svg.append("clipPath")
          .attr("id", "clip-above")
        .append("path")
          .attr("d", area.y0(0));

      svg.append("path")
          .attr("class", "area above")
          .attr("clip-path", "url(#clip-above)")
          .attr("d", area.y0(function(d) { return y(d["average"]); }));

      svg.append("path")
          .attr("class", "area below")
          .attr("clip-path", "url(#clip-below)")
          .attr("d", area);

      svg.append("path")
          .attr("class", "line")
          .attr("d", line);

      svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(0," + height + ")")
          .call(xAxis);

      svg.append("g")
          .attr("class", "y axis")
          .call(yAxis)
        .append("text")
          .attr("transform", "rotate(-90)")
          .attr("y", 6)
          .attr("dy", ".71em")
          .style("text-anchor", "end")
          .text("Grades");
    });



    </script>
    </form>
like image 82
John J. Ryan Avatar answered Sep 29 '22 16:09

John J. Ryan