Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
c3-closed
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
charts
c3-closed
Commits
14e92c54
Commit
14e92c54
authored
Sep 23, 2014
by
Masayuki Tanaka
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #555 from yuvii/master
phantomjs based exporter for c3 charting library
parents
60fc19a2
4f09ae93
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
153 additions
and
0 deletions
+153
-0
config.json
extensions/exporter/config.json
+12
-0
phantom-exporter.js
extensions/exporter/phantom-exporter.js
+141
-0
test.png
extensions/exporter/test.png
+0
-0
No files found.
extensions/exporter/config.json
0 → 100644
View file @
14e92c54
{
"js"
:
[
"../../bower_components/d3/d3.min.js"
,
"../../c3.min.js"
],
"css"
:
[
"../../c3.css"
],
"template"
:
"<html><head><meta charset=
\"
utf-8
\"
><style>{0}</style></head><body><div id=
\"
chart
\"
></div></body></html>"
}
\ No newline at end of file
extensions/exporter/phantom-exporter.js
0 → 100644
View file @
14e92c54
/**
* PNG\JPEG exporter for C3.js, version 0.2
* (c) 2014 Yuval Bar-On
*
* usage: path/to/phantomjs output options [WxH]
*
*/
// useful python-styled string formatting, "hello {0}! Javascript is {1}".format("world", "awesome");
if
(
!
String
.
prototype
.
format
)
{
String
.
prototype
.
format
=
function
()
{
var
args
=
arguments
;
return
this
.
replace
(
/{
(\d
+
)
}/g
,
function
(
match
,
number
)
{
return
typeof
args
[
number
]
!=
'undefined'
?
args
[
number
]
:
match
;
});
};
}
// defaults
var
page
=
require
(
'webpage'
).
create
(),
fs
=
require
(
'fs'
),
system
=
require
(
'system'
),
config
=
JSON
.
parse
(
fs
.
read
(
'config.json'
)
),
output
,
size
;
if
(
system
.
args
.
length
<
3
)
{
console
.
log
(
'Usage: phantasm.js filename html [WxH]'
);
phantom
.
exit
(
1
);
}
else
{
out
=
system
.
args
[
1
];
opts
=
JSON
.
parse
(
system
.
args
[
2
]
);
if
(
system
.
args
[
3
])
{
var
dimensions
=
system
.
args
[
3
].
split
(
'x'
),
width
=
dimensions
[
0
],
height
=
dimensions
[
1
];
function
checkNum
(
check
)
{
check
=
parseInt
(
check
);
if
(
!
isNaN
(
check
))
return
check
;
return
false
;
}
width
=
checkNum
(
width
);
height
=
checkNum
(
height
);
if
(
width
&&
height
)
{
page
.
viewportSize
=
{
height
:
height
,
width
:
width
}
}
// fit chart size to img size, if undefined
if
(
!
opts
.
size
)
{
opts
.
size
=
{
"height"
:
height
,
"width"
:
width
};
}
}
else
{
// check if size is defined in chart,
// else apply defaults
page
.
viewportSize
=
{
height
:
(
opts
.
size
&&
opts
.
size
.
height
)
?
opts
.
size
.
height
:
320
,
width
:
(
opts
.
size
&&
opts
.
size
.
width
)
?
opts
.
size
.
width
:
710
,
}
}
}
page
.
onResourceRequested
=
function
(
requestData
,
request
)
{
console
.
log
(
'::loading resource '
,
requestData
[
'url'
]);
};
// helpful debug functions
page
.
onConsoleMessage
=
function
(
msg
){
console
.
log
(
msg
);
};
page
.
onError
=
function
(
msg
,
trace
)
{
var
msgStack
=
[
'ERROR: '
+
msg
];
if
(
trace
&&
trace
.
length
)
{
msgStack
.
push
(
'TRACE:'
);
trace
.
forEach
(
function
(
t
)
{
msgStack
.
push
(
' -> '
+
t
.
file
+
': '
+
t
.
line
+
(
t
.
function
?
' (in function "'
+
t
.
function
+
'")'
:
''
));
});
}
console
.
error
(
msgStack
.
join
(
'
\
n'
));
};
// render page
function
injectVerify
(
script
)
{
var
req
=
page
.
injectJs
(
script
);
if
(
!
req
)
{
console
.
log
(
'
\
nError!
\
n'
+
script
+
' not found!
\
n'
);
phantom
.
exit
(
1
);
}
}
page
.
onLoadFinished
=
function
()
{
console
.
log
(
'::rendering'
);
for
(
var
j
in
config
.
js
)
{
injectVerify
(
config
.
js
[
j
]);
}
page
.
evaluate
(
function
(
chartoptions
)
{
// phantomjs doesn't know how to handle .bind, so we override
Function
.
prototype
.
bind
=
Function
.
prototype
.
bind
||
function
(
thisp
)
{
var
fn
=
this
;
return
function
()
{
return
fn
.
apply
(
thisp
,
arguments
);
};
};
// generate chart
c3
.
generate
(
chartoptions
);
},
opts
);
// setting transition to 0 has proven not to work thus far, but 300ms isn't much
// so this is acceptable for now
setTimeout
(
function
()
{
page
.
render
(
out
);
phantom
.
exit
();
},
300
);
}
// apply css inline because that usually renders better
var
css
=
''
;
for
(
var
i
in
config
.
css
)
{
css
+=
fs
.
read
(
config
.
css
[
i
]);
}
page
.
content
=
config
.
template
.
format
(
css
);
\ No newline at end of file
extensions/exporter/test.png
0 → 100644
View file @
14e92c54
16.1 KB
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment