1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package file;
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList;
public class SaveJs {
public static boolean initIndex(String fileName) throws IOException{ File file = new File(fileName); if(!file.getParentFile().exists()){ System.out.println("不存在"+fileName+"的目录,进行创建"); if(!file.getParentFile().mkdirs()){ System.out.println("创建目标文件目录失败"); return false; } } try { if(!file.exists()){ if(!file.createNewFile()){ return false; } } } catch (IOException e) { e.printStackTrace(); } writefunc(fileName); return true; }
private static void writefunc(String fileName) throws IOException{ FileWriter fw = new FileWriter(fileName,false); fw.write("function draw(name,xRange,yRange,xText,yText,data,scaleN){\n"); fw.write(" document.writeln(name);\n"); fw.write(" var width = 500, height = 250,margin = {left:50,top:30,right:20,bottom:20},g_width = width - margin.left -margin.right,g_height = height -margin.top - margin.bottom;\n"); fw.write(" var container = d3.select('body').append('container');\n"); fw.write(" var svg = container.append('svg').attr('width',width).attr('height',height);\n"); fw.write("var g = svg.append('g').attr('transform','translate('+margin.left+','+margin.top+')');\n"); fw.write("var scale_x = d3.scaleLinear().domain([0,xRange]).range([0,g_width])\n"); fw.write(" var scale_y = d3.scaleLinear().domain([0,yRange]).range([g_height,0])\n"); fw.write("var line_generator = d3.line()\n"); fw.write(".x(function(d,i){return scale_x(i)/scaleN;})\n"); fw.write(".y(function(d){return scale_y(d);})\n"); fw.write("g.append('path').attr('d',line_generator(data))\n"); fw.write(" var x_axis =d3.axisBottom(scale_x),y_axis =d3.axisLeft(scale_y);\n"); fw.write("g.append('g').call(x_axis).attr('transform','translate(0,'+g_height+')').append('text').text(xText).attr('dx','40em').attr('dy','-1em')\n"); fw.write("g.append('g').call(y_axis).append('text').text(yText).attr('text-anchor','start').attr('dx','1em').attr('dy','1em')\n"); fw.write("\n\n"); fw.write(" var wenzi = svg.append('text').attr('font-family', 'sans-serif').attr('font-size', '11px')"); fw.write(".attr('fill', 'red').attr('class','move2')\n"); fw.write("var yuan = svg.append('circle').attr('r', 1).attr('class','move1');\n"); fw.write("svg.on('mousemove',function(event){\n"); fw.write("var event = event || window.event;\n"); fw.write("yuan.attr('style','display:block');\n"); fw.write("wenzi.attr('style','display:block'); \n"); fw.write("yuan.attr('cx',event.offsetX)\n"); fw.write("yuan.attr('cy',event.offsetY);\n"); fw.write("if(event.offsetX <=450){\n"); fw.write("wenzi.text(function(d) {\n"); fw.write("return ((event.offsetX-50)/g_width*xRange).toFixed(2) + \",\" + ((height-event.offsetY-20)/g_height*yRange).toFixed(2);\n"); fw.write("})\n"); fw.write("wenzi.attr('x', event.offsetX+7)\n"); fw.write("wenzi.attr('y', event.offsetY+7)\n"); fw.write("}else{\n"); fw.write("wenzi.text(function(d) {\n"); fw.write("return ((event.offsetX-50)/g_width*xRange).toFixed(2) + \",\" + ((height-event.offsetY-20)/g_height*yRange).toFixed(2);\n"); fw.write("})\n"); fw.write("wenzi.attr('x', event.offsetX-50)\n"); fw.write("wenzi.attr('y', event.offsetY+7)\n"); fw.write("}})\n"); fw.write("}\n"); fw.close(); }
public static void drawLineChart(String fileName,String name, int xRange, int yRange,String xText,String yText,ArrayList data,int scaleN) throws IOException{ FileWriter fw = new FileWriter(fileName,true); fw.write("var data = "+data+"\n\n"); fw.write("var xRange="+xRange+",yRange="+yRange+",name='<p>"+name); fw.write("</p>',xText='"+xText+"',yText='"+yText+"',scaleN="+scaleN+";\n"); fw.write("draw(name,xRange,yRange,xText,yText,data,scaleN);\n"); fw.close(); } }
|