arcgauge = conf => {
const gauge = {};
const _f1 = d3.format(".1f");
const default_config = {
min:0,
max:1,
value:0,
bgRadius:37,
bgArcWidth:4,
bgColor:"none",
ftRadius:8,
ftArcWidth:23,
ftColor:"#1ca9e4",
startAngle:0,
endAngle:6.283,
cornerRadius:0,
title:"Tempture",
status:[{mapping_key:"0",text:"L",startAngle:3.14,endAngle:6.283},{mapping_key:"1",text:"R",startAngle:0,endAngle:3.14}],
width:100,
height:100,
valueX:0,
valueY:20,
valueSize:28,
x:0,
y:0};
const _config = conf ? conf : default_config;
gauge.updateShape = function(node,config){
d3.select(node).selectAll("*").remove();
this.render(node,config) ;
};
gauge.updateValue = function(node,v){
node.value = v;
let g = d3.select(node);
//node.bgconf[1].endAngle = node.scale(v);
let status = node.config.status ;
status.forEach(d=>{
if( d.mapping_key == v ){
node.bgconf[1].startAngle = d.startAngle ;
node.bgconf[1].endAngle = d.endAngle ;
if(d.ftColor) node.bgconf[1].color = d.ftColor ;
}
})
g.selectAll("path").data(node.bgconf,function(d){return d.id;})
.attr("d",function(d){
return d3.arc().innerRadius(d.innerR).outerRadius(d.outerR).cornerRadius(d.cornerRadius)
.startAngle(d.startAngle).endAngle(d.endAngle)();
})
.attr("stroke",function(d){return d.color;})
.attr("fill",function(d){return d.color;})
;
};
gauge.render = function(node,config){
config = Object.assign({},_config,config);
node.scale = d3.scaleLinear().domain([config.min, config.max]).range([config.startAngle, config.endAngle]);
node.scale.clamp(true) ;
node.config = config;
node.bgconf = [
{id: "bg", innerR: config.bgRadius, outerR: config.bgRadius+config.bgArcWidth, startAngle: config.startAngle, endAngle: config.endAngle, color: config.bgColor, cornerRadius:config.cornerRadius },
{id: "ft", innerR: config.ftRadius, outerR: config.ftRadius+config.ftArcWidth, startAngle: config.startAngle, endAngle: config.endAngle, color: config.ftColor, cornerRadius:config.cornerRadius }
];
let g=d3.select(node)
.attr("transform","scale(2) translate("+[config.x,config.y]+")") ;
//draw background and title
g.append("rect")
//.attr("style", "cursor:move; opacity:0.7; fill: #FF9000")
.attr("fill","none")
.attr("stroke-width",0.3)
.attr("stroke","#fcfcfc")
.attr("width", config.width)
.attr("height", config.height)
;
var corner1_width=2 , corner1_height=2;
g.selectAll("rect.corner1")
.data([[0,0],[config.width,0],[config.width,config.height],[0,config.height]])
.enter()
.append("rect")
.attr("class","corner1")
.attr("fill","#fcfcfc")
.attr("width", corner1_width)
.attr("height", corner1_height)
.attr("x",d=>d[0]-corner1_width/2)
.attr("y",d=>d[1]-corner1_height/2)
var corner2_width=4, corner2_height=4, inner_offsetX=10, inner_offsetY=10;
g.selectAll("rect.corner2")
.data([[0+inner_offsetX,0+inner_offsetY],[config.width-inner_offsetY,0+inner_offsetY],[config.width-inner_offsetX,config.height-inner_offsetY],[0+inner_offsetX,config.height-inner_offsetY]])
.enter()
.append("rect")
.attr("class","corner2")
.attr("fill","#454545")
.attr("width", corner2_width)
.attr("height", corner2_height)
.attr("x",d=>d[0]-corner2_width/2)
.attr("y",d=>d[1]-corner2_height/2)
var tilte_offsetX=5, tilte_offsetY=-14;
var title_width=config.width-tilte_offsetX*2 ,title_height=12;
g.selectAll("rect.title")
.data([[tilte_offsetX,title_width,"#fcfcfc"],[tilte_offsetX+1,title_width-2,"#434343"]])
.enter()
.append("rect")
.attr("class","title")
.attr("fill",d=>d[2])
.attr("width",d=>d[1])
.attr("height",title_height)
.attr("x",d=>d[0])
.attr("y",tilte_offsetY)
;
//bgRadius, bgArcWidth
g.selectAll("circle")
.data([
{r:config.bgRadius+config.bgArcWidth+3, stroke:"#454545", "stroke-width":5, fill:"none"},
{r:config.bgRadius+config.bgArcWidth+1, stroke:"#fafafa", "stroke-width":0.5, fill:"none"},
{r:config.bgRadius+config.bgArcWidth-config.bgArcWidth/2, stroke:config.ftColor, "stroke-width":config.bgArcWidth, fill:"none"},
{r:config.bgRadius-1, stroke:"#fafafa", "stroke-width":0.7, fill:"none"},
{r:config.bgRadius-4, stroke:"#808080", "stroke-width":0.5, fill:"rgba(32,32,32,0.8)"}
])
.enter()
.append("circle")
.attr("cx",config.width/2)
.attr("cy",config.height/2)
.attr("r",d=> d3.max(d.r,0) )
.attr("stroke",d=>d.stroke)
.attr("stroke-width",d=>d["stroke-width"])
.attr("fill",d=>d.fill)
;
//draw arc
node.bgconf[1].endAngle = node.scale(config.value);
g.append("g")
.attr("transform","translate("+[config.width/2,config.height/2]+")")
.selectAll("path")
.data(node.bgconf,function(d){return d.id;})
.enter().append("path")
.attr("d",function(d){
return d3.arc().innerRadius(d.innerR).outerRadius(d.outerR).cornerRadius(d.cornerRadius)
.startAngle(d.startAngle).endAngle(d.endAngle)();
})
.attr("stroke",function(d){return d.color;})
.attr("fill",function(d){return d.color;})
;
//draw text
var title_font_size = 10;
g.append("text")
.attr("class","tempText unselectable")
.attr("text-anchor","middle")
.attr("fill","#FFF")
.attr("font-size",title_font_size)
.attr("x",tilte_offsetX + title_width/2)
.attr("y",tilte_offsetY + title_font_size-1)
.text(config.title);
var text_position = config.ftArcWidth+config.ftRadius-10 ;
g.selectAll("text.status_text")
.data(config.status)
.enter()
.append("text")
.attr("class","status_text unselectable")
.attr("text-anchor","middle")
.attr("fill","#454545")
.attr("font-size",title_font_size)
.attr("x", (d,i)=>config.width/2 + (i==0? -text_position:text_position) )
.attr("y", config.height/2+title_font_size/2)
.text(d=>d.text);
}
return gauge ;
}