query-a7b3eacbbfa98fda938c9cf8d877627a

rq turtle/ttl

(56) Scatter chart: characters in Water Margin chapters by narrative roleNo.56 (2020.12.17T1304 created)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# scatter chart of characters in Water Margin chapters classified by narrative role
# also viewable as table
#defaultView:ScatterChart
### Note: Points are distinguished into different colors according to the 3rd non-item variable (?narrative_roleLabel).
SELECT DISTINCT ?chapter ?chapter_number ?name ?character ?first_appearance ?narrative_roleLabel
WHERE {
  # ?chapter is instance of (P31) chapter (Q1980247)
  ?chapter wdt:P31 wd:Q1980247;
           # and is part of (P361) [OneOrMorePath (+)] Water Margin's 120-chapter edition (Q70761009)
           wdt:P361+ wd:Q70761009
  # optional: ?chapter's chapter (P792) is ?number
  ### Note: This OPTIONAL (generating ?number) and the next two OPTIONALs (generating respectively ?first_appearance_number and ?native_name)
  ### produce respectively ?chapter_number and ?name, the variables for X-axis and Y-axis. Although
  ### the variable for an axis should not have null value in the 1st record (row) of the table,
  ### putting ?number, ?first_appearance_number, and ?native_name into OPTIONAL makes it easy
  ### to check (in table view) whether there is any ?chapter lacking number or ?character lacking first appearance or native name.
  OPTIONAL {?chapter wdt:P792 ?number}
  # use IF to bind ?number's 3-character form as ?chapter_number:
  # if ?number has 1 character, concatenate "00" and it;
  ### Note: Getting chapter's 3-character number for table view because ?number, whose data type is string (see P792 (chapter)),
  ### cannot be ordered as number in table's column although
  ### it can be ordered as number in scatter chart's axis.
  BIND(IF(STRLEN(?number)=1, CONCAT("00", ?number),
       # if ?number has 2 characters, concatenate "0" and it;
       IF(STRLEN(?number)=2, CONCAT("0", ?number),
       # otherwise (?number has 3 characters), don't change it
       ?number)) AS ?chapter_number)
  # ?chapter's "characters" (P674) statement is ?y
  ?chapter p:P674 ?y.
  # ?y's value is ?character
  ?y ps:P674 ?character
  # optional: ?first_appearance is instance of (P31) chapter (Q1980247)
  OPTIONAL {?first_appearance wdt:P31 wd:Q1980247;
                              # and is part of (P361) [OneOrMorePath (+)] Water Margin's 120-chapter edition (Q70761009)
                              wdt:P361+ wd:Q70761009
            # optional: ?first_appearance's chapter (P792) is ?first_appearance_number
            OPTIONAL {?first_appearance wdt:P792 ?first_appearance_number}
            # ?first_appearance's "characters" (P674) statement is ?y1
            ?first_appearance p:P674 ?y1.
            # ?y1's value is ?character
            ?y1 ps:P674 ?character;
                # ?y1's qualifier "subject has role" (P2868) is "first appearance" (Q8563381)
                pq:P2868 wd:Q8563381}
  # use IF to bind ?first_appearance_number's 3-character form as ?first_appearance_number1:
  # if ?first_appearance_number has 1 character, concatenate "00" and it;
  BIND(IF(STRLEN(?first_appearance_number)=1, CONCAT("00", ?first_appearance_number),
       # if ?first_appearance_number has 2 characters, concatenate "0" and it;
       IF(STRLEN(?first_appearance_number)=2, CONCAT("0", ?first_appearance_number),
       # otherwise (?first_appearance_number has 3 characters), don't change it
       ?first_appearance_number)) AS ?first_appearance_number1)
  # optional: ?character's native label (P1705) or name in native language (P1559) is ?native_name
  ### Note: Native label (P1705) is for fictional non-mythical animal and group of fictional characters,
  ### which should not have name in native language (P1559),
  ### and is also for those fictional humans or characters who do not have a (personal) name.
  OPTIONAL {?character (wdt:P1705|wdt:P1559) ?native_name}
  # bind concatenation of ?first_appearance_number1 and ?native_name as ?name
  ### Note: Although a string of number (e.g., ?number above) can be ordered as number in scatter chart's axis (not in table's column),
  ### a string that consists of a number followed by other characters cannot be ordered as number first in scatter chart's axis.
  ### So ?first_appearance_number has to be transformed to ?first_appearance_number1 (3-character form) before concatenated with ?native_name.
  BIND(CONCAT(?first_appearance_number1, ?native_name) AS ?name)
  # optional: show ?y's qualifier "narrative role" (P5800) as ?narrative_role
  OPTIONAL {?y pq:P5800 ?narrative_role}
  # show label in auto language as default, and English when no default label exists
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
### Note: ORDER BY the 3rd variable in DESC order because
### if the 3rd variable does not have value in the 1st record (row) of the table,
### points in scatter chart cannot be distinguished into different colors according to the 3rd variable.
ORDER BY DESC(?narrative_roleLabel)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?chapter"):::projected v4("?chapter_number"):::projected v6("?character"):::projected v7("?first_appearance"):::projected v9("?first_appearance_number") v10("?first_appearance_number1") v12("?name"):::projected v13("?narrative_role") v1("?narrative_roleLabel"):::projected v11("?native_name") v3("?number") v5("?y") v8("?y1") c9(["wd:Q8563381"]):::iri c14(["bd:serviceParam"]):::iri c16(["#91;AUTO_LANGUAGE#93;,en"]):::literal c2(["wd:Q1980247"]):::iri c4(["wd:Q70761009"]):::iri v2 --"p:direct/P31"--> c2 v2 --"p:direct/P361"--> c4 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v2 -."p:direct/P792".-> v3 end bind0[/"if(string-length(?number) = '1^^xsd:integer',concat('00',?number),if(string-length(?number) = '2^^xsd:integer',concat('0',?number),?number))"/] v3 --o bind0 bind0 --as--o v4 v2 --"p:P674"--> v5 v5 --"p:statement/P674"--> v6 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:direct/P31".-> c2 v7 --"p:direct/P361"--> c4 v7 --"p:P674"--> v8 v8 --"p:statement/P674"--> v6 v8 --"p:qualifier/P2868"--> c9 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:direct/P792".-> v9 end end bind1[/"if(string-length(?first_appearance_number) = '1^^xsd:integer',concat('00',?first_appearance_number),if(string-length(?first_appearance_number) = '2^^xsd:integer',concat('0',?first_appearance_number),?first_appearance_number))"/] v9 --o bind1 bind1 --as--o v10 subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v6 -."p:direct/P1559".-> v11 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v6 --"p:direct/P1705"--> v11 end union0r <== or ==> union0l end end bind2[/"concat(?first_appearance_number1,?native_name)"/] v10 --o bind2 v11 --o bind2 bind2 --as--o v12 subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; v5 -."p:qualifier/P5800".-> v13 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c14 --"wikibase:language"--> c16 end