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?
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.
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?
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With