query-78380a4623c28497504b399b56225b13

rq turtle/ttl

(65) Line Chart: numbers of (new or all) characters in Dream of the Red Chamber chaptersNo.65 (2021.10.09T2301 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#>
# numbers of (new or all) characters in Dream of the Red Chamber chapters
# viewed as line chart (also viewable as table)
#defaultView:LineChart
SELECT DISTINCT ?chapter ?chapter_number ?character_count
WHERE {
  {SELECT DISTINCT ?chapter (COUNT(DISTINCT ?character) AS ?character_count)
   WHERE {
     # ?chapter is instance of (P31) chapter (Q1980247)
     ?chapter wdt:P31 wd:Q1980247;
              # and is part of (P361) [OneOrMorePath (+)] Dream of the Red Chamber (Q8265)
              wdt:P361+ wd:Q8265
     # optional: ?chapter's "characters" (P674) statement is ?x_character
     OPTIONAL {?chapter p:P674 ?x_character.
               # ?x_character's value is ?character
               ?x_character ps:P674 ?character;
                            # ?x_character's qualifier "subject has role" (P2868) is "first appearance" (Q8563381)
                            ### Note: This line can be removed, and ?character_count will be changed to count of
                            ### all characters (not only new characters).
                            pq:P2868 wd:Q8563381}
   }
   # group COUNT by ?chapter
   GROUP BY ?chapter
  }
  # optional: ?chapter's chapter (P792) is ?number
  OPTIONAL {?chapter wdt:P792 ?number}
  # use IF to bind ?number's 4-character form as ?chapter_number:
  # if ?number has 1 character, concatenate "100" and it;
  ### Note: Because data type of the Y-axis variable (?character_count) is quantity,
  ### ?number, whose data type is string, needs to be transformed to numeric strings with the same number of characters,
  ### and the first character should not be "0".
  ### Otherwise numbers on X-axis will be ordered descendingly per their corresponding values on Y-axis,
  ### not ascendingly per their own values.
  BIND(IF(STRLEN(?number)=1, CONCAT("100", ?number),
       # if ?number has 2 characters, concatenate "10" and it;
       IF(STRLEN(?number)=2, CONCAT("10", ?number),
       # otherwise (?number has 3 characters), concatenate "1" and it
       CONCAT("1", ?number))) AS ?chapter_number)
  # show label in auto language as default, and English when no default label exists
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v1("?chapter"):::projected v6("?chapter_number"):::projected v3("?character") v4("?character_count"):::projected v5("?number") v2("?x_character") c4(["wd:Q8265"]):::iri c8(["wd:Q8563381"]):::iri c11(["bd:serviceParam"]):::iri c13(["#91;AUTO_LANGUAGE#93;,en"]):::literal c2(["wd:Q1980247"]):::iri v1 --"p:direct/P31"--> c2 v1 --"p:direct/P361"--> c4 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v1 -."p:P674".-> v2 v2 --"p:statement/P674"--> v3 v2 --"p:qualifier/P2868"--> c8 end bind1[/"count(?character)"/] v3 --o bind1 bind1 --as--o v4 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v1 -."p:direct/P792".-> v5 end bind2[/"if(string-length(?number) = '1^^xsd:integer',concat('100',?number),if(string-length(?number) = '2^^xsd:integer',concat('10',?number),concat('1',?number)))"/] v5 --o bind2 bind2 --as--o v6 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c11 --"wikibase:language"--> c13 end