I need to create a triangle which called a duval triangle. It looks like this:
I need to do it with canvas. so far I succeed to create a triangle. But I don\'t know
Disclaimer: I don't have a background in Chemical Engineering -- so bear with me. :-)
I googled this description of implementing a Duval Triangles, but I found the paper beyond my knowledge of Electro-Chemistry. So as a workaround I built snippets to help you draw your Duval Triangle since you already have the required knowledge.
Drawing the segments inside the Duval Triangle (D1,D2,DT,PD,T1,T2,T3)
Assume you define your triangle segments in objects like this:
// the definition of the D2 segment
{
// The vertices that outline the D2 segment
points:[{x:385,y:366},{x:201,y:366},{x:324,y:150},{x:356,y:204},{x:321,y:256}],
// The fill color of the D2 segment
fill:'deepskyblue',
// The label for the D2 segment
// By default, the label is inside the segment,
// but can be outside with a connecting line
label:{text:'D2',cx:290,cy:290,withLine:false,endX:null,endY:null},
},
You can draw the segment using that segment-object definition using the drawSegment
function. See the example code at the bottom of this post.
Drawing the triangle outline and the percentage tickmarks (20,40,60,80)
Assume you define your total triangle like this:
// define the 3 vertices of the triangle
var v0={x:114,y:366};
var v1={x:306,y:30};
var v2={x:498,y:366};
var triangle=[v0,v1,v2];
You can draw the triangle using the drawTriangle
function and draw the tick marks using the ticklines
function in the example code.
Drawing the molecule labels
The same triangle definition is used to draw the molecule labels (and arrows) outside the triangle using the moleculeLabel
function in the example code.
Drawing the chart legend
Assuming you define the text of the chart legend like this:
var legendTexts=[
'PD = Partial Discharge',
'T1 = Thermal fault < 300 celcius',
'...'
];
You can draw the chart legend using the drawLegend
function in the example code.
Putting it all together in an example:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
// https://www.researchgate.net/publication/4345236_A_Software_Implementation_of_the_Duval_Triangle_Method
var v0={x:114,y:366};
var v1={x:306,y:30};
var v2={x:498,y:366};
var triangle=[v0,v1,v2];
// Define all your segments here
var segments=[
{
points:[{x:114,y:366},{x:281,y:76},{x:324,y:150},{x:201,y:366}],
fill:'rgb(172,236,222)',
label:{text:'D1',cx:200,cy:290,withLine:false,endX:null,endY:null},
},
{
points:[{x:385,y:366},{x:201,y:366},{x:324,y:150},{x:356,y:204},{x:321,y:256}],
fill:'deepskyblue',
label:{text:'D2',cx:290,cy:290,withLine:false,endX:null,endY:null},
},
{
points:[{x:297,y:46},{x:392,y:214},{x:372,y:248},{x:441,y:366},{x:385,y:366},{x:321,y:256},{x:356,y:204},{x:281,y:76}],
fill:'lightCyan',
label:{text:'DT',cx:370,cy:290,withLine:false,endX:366,endY:120},
},
{
points:[{x:306,y:30},{x:312,y:40},{x:300,y:40}],
fill:'black',
label:{text:'PD',cx:356,cy:40,withLine:true,endX:321,endY:40},
},
{
points:[{x:312,y:40},{x:348,y:103},{x:337,y:115},{x:297,y:46},{x:300,y:40}],
fill:'navajoWhite',
label:{text:'T1',cx:375,cy:70,withLine:true,endX:340,endY:75},
},
{
points:[{x:348,y:103},{x:402,y:199},{x:392,y:214},{x:337,y:115}],
fill:'tan',
label:{text:'T2',cx:400,cy:125,withLine:true,endX:366,endY:120},
},
{
points:[{x:402,y:199},{x:498,y:366},{x:441,y:366},{x:372,y:248}],
fill:'peru',
label:{text:'T3',cx:425,cy:290,withLine:false,endX:null,endY:null},
},
];
// label styles
var labelfontsize=12;
var labelfontface='verdana';
var labelpadding=3;
// pre-create a canvas-image of the arrowhead
var arrowheadLength=10;
var arrowheadWidth=8;
var arrowhead=document.createElement('canvas');
premakeArrowhead();
var legendTexts=['PD = Partial Discharge','T1 = Thermal fault < 300 celcius','...'];
// start drawing
/////////////////////
// draw colored segments inside triangle
for(var i=0;i
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red; margin:0 auto; }