Continuing on the theme of using D3js to visualize RapidMiner results, this time I show you how to skip putting it into a RapidMiner Server dashboard. I simply borrowed the scatterplot D3js code from mblocks and slapped into a RapidMiner process (see XML below). Copy the XML code and you can make a D3js Scatter Plot in RapidMiner!
For this example, I’m just using the Iris data set but I have tried it with shapefiles and other data. I also use RapidMiner macros to vary the x and y axis. You can control those macros within the context view of RapidMiner.
When you execute the process, it writes two files to a tmp directory, the Iris data set in a data.js file and the actual scatterplot HTML file. The data.js file is called into the HTML file.
Note: you will need some sort of web server to be running so you can render the generated HTML file correctly. Enjoy!
<?xml version="1.0" encoding="UTF-8"?><process version="7.2.003"> <context> <input/> <output/> <macros> <macro> <key>x_axis</key> <value>a2</value> </macro> <macro> <key>y_axis</key> <value>a3</value> </macro> <macro> <key>color_column</key> <value>label</value> </macro> </macros> </context> <operator activated="true" class="process" compatibility="7.2.003" expanded="true" name="Process"> <process expanded="true"> <operator activated="true" class="text:create_document" compatibility="7.2.001" expanded="true" height="68" name="Create Document (3)" width="90" x="45" y="255"> <parameter key="text" value="<!DOCTYPE html> <html> <meta charset="utf-8"> <!-- Example based on http://bl.ocks.org/mbostock/3887118 --> <!-- Tooltip example from http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html --> <style> body { font: 11px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .dot { stroke: #000; } .tooltip { position: absolute; width: 200px; height: 28px; pointer-events: none; } </style> <body> <script src="http://d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; /* * value accessor - returns the value to encode for a given data object. * scale - maps value to a visual display encoding, such as a pixel position. * map function - maps from data value to display value * axis - sets up axis */ // setup x var xValue = function(d) { return d.%{x_axis};}, // data -> value xScale = d3.scale.linear().range([0, width]), // value -> display xMap = function(d) { return xScale(xValue(d));}, // data -> display xAxis = d3.svg.axis().scale(xScale).orient("bottom"); // setup y var yValue = function(d) { return d.%{y_axis};}, // data -> value yScale = d3.scale.linear().range([height, 0]), // value -> display yMap = function(d) { return yScale(yValue(d));}, // data -> display yAxis = d3.svg.axis().scale(yScale).orient("left"); // setup fill color var cValue = function(d) { return d.%{color_column};}, color = d3.scale.category10(); // add the graph canvas to the body of the webpage var svg = d3.select("body").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 + ")"); // add the tooltip area to the webpage var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // load data d3.json("data.js", function(error, data) { // change string (from CSV) into number format data.forEach(function(d) { d.%{x_axis} = +d.%{x_axis}; d.%{y_axis} = +d.%{y_axis}; // console.log(d); }); // don't want dots overlapping axis, so add in buffer to data domain xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]); // x-axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("%{x_axis}"); // y-axis svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("%{y_axis}"); // draw dots svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", 3.5) .attr("cx", xMap) .attr("cy", yMap) .style("fill", function(d) { return color(cValue(d));}) .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(d["%{color_column}"] + "<br/> (" + xValue(d) + ", " + yValue(d) + ")") .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0); }); // draw legend var legend = svg.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); // draw legend colored rectangles legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); // draw legend text legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d;}) }); </script> </body> </html>"/> <description align="center" color="transparent" colored="false" width="126">D3js Scatterplot script</description> </operator> <operator activated="true" class="text:write_document" compatibility="7.2.001" expanded="true" height="82" name="Write Document" width="90" x="179" y="255"/> <operator activated="true" class="write_file" compatibility="7.2.003" expanded="true" height="68" name="Write File" width="90" x="313" y="255"> <parameter key="filename" value="C:tmpD3IRISscatterplot.html"/> <description align="center" color="transparent" colored="false" width="126">Write HTML with D3js</description> </operator> <operator activated="true" class="retrieve" compatibility="7.2.003" expanded="true" height="68" name="Retrieve Iris" width="90" x="45" y="30"> <parameter key="repository_entry" value="//Samples/data/Iris"/> <description align="center" color="transparent" colored="false" width="126">Load Iris Data</description> </operator> <operator activated="true" class="text:data_to_json" compatibility="7.2.001" expanded="true" height="82" name="Data To JSON" width="90" x="179" y="30"> <parameter key="generate_array" value="true"/> <description align="center" color="transparent" colored="false" width="126">Convert to JSON</description> </operator> <operator activated="true" class="text:write_document" compatibility="7.2.001" expanded="true" height="82" name="Write Document (2)" width="90" x="313" y="30"> <description align="center" color="transparent" colored="false" width="126">Write JSON data for D3plt</description> </operator> <operator activated="true" class="write_file" compatibility="7.2.003" expanded="true" height="68" name="Write File (2)" width="90" x="581" y="120"> <parameter key="filename" value="C:tmpD3data.js"/> <description align="center" color="transparent" colored="false" width="126">JS data file</description> </operator> <operator activated="true" class="text:documents_to_data" compatibility="7.2.001" expanded="true" height="82" name="Documents to Data" width="90" x="581" y="30"> <parameter key="text_attribute" value="text"/> </operator> <operator activated="true" class="extract_macro" compatibility="7.2.003" expanded="true" height="68" name="Extract Macro" width="90" x="715" y="30"> <parameter key="macro" value="jsonData"/> <parameter key="macro_type" value="data_value"/> <parameter key="attribute_name" value="text"/> <parameter key="example_index" value="1"/> <list key="additional_macros"/> </operator> <connect from_op="Create Document (3)" from_port="output" to_op="Write Document" to_port="document"/> <connect from_op="Write Document" from_port="file" to_op="Write File" to_port="file"/> <connect from_op="Write File" from_port="file" to_port="result 1"/> <connect from_op="Retrieve Iris" from_port="output" to_op="Data To JSON" to_port="example set 1"/> <connect from_op="Data To JSON" from_port="documents" to_op="Write Document (2)" to_port="document"/> <connect from_op="Write Document (2)" from_port="document" to_op="Documents to Data" to_port="documents 1"/> <connect from_op="Write Document (2)" from_port="file" to_op="Write File (2)" to_port="file"/> <connect from_op="Documents to Data" from_port="example set" to_op="Extract Macro" to_port="example set"/> <portSpacing port="source_input 1" spacing="0"/> <portSpacing port="sink_result 1" spacing="0"/> <portSpacing port="sink_result 2" spacing="0"/> </process> </operator> </process>